我正在尝试从下拉列表中选择一个值,并从 angularJs 中相应的选择 id 中获取数据
我想出了 angular-ui/ui-select指令
<ui-select ng-model="customer" theme="selectize">
<ui-select-match placeholder="Customer List">{{$select.selected.Name}}</ui-select-match>
<ui-select-choices repeat="customer in customers | filter: $select.search">
<span ng-bind-html="customer.Name | highlight: $select.search"></span>
<small ng-bind-html="customer.Id.toString() | highlight: $select.search"></small>
</ui-select-choices>
</ui-select>
在我的 Controller 上我有获取客户的方法
$scope.setActiveCustomer = function(customer) {
customersService.getCustomers(customer).then(function(response) {
$scope.selectedCustomer = response;
});
};
我的问题是
一旦我选择了客户,我应该使用哪个事件来触发 setActiveCustomer(比如 onchange 之类的)。当我使用
ng-click
事件触发多次,因为ng-click
用于控制激活。另一件事是如何为 ui-select 设置初始值?
谢谢
最佳答案
根据ui-select FAQ ,你的 ng-model
表达式应该有一个 .
在里面,例如:
<ui-select ng-model="model.customer" theme="selectize">
要设置初始值,您可以只设置 ng-model
中指定的属性,因此在您的 Controller 中:
$scope.model = {
customer: $scope.customers[0] // set the initial selected option
};
对于 onchange 事件,您可以像这样使用 $scope.$watch()
:
$scope.$watch('model.customer', function (customer) {
$scope.setActiveCustomer(customer);
});
希望这对您有所帮助。
关于angularjs - 向 ui.select angular 上的 Controller 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325563/