我正在尝试从下拉列表中选择一个值,并从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>
在我的控制器上我得到了客户方法
$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中指定的属性,因此在控制器中:
$scope.model = { customer: $scope.customers[0] // set the initial selected option };
对于onchange事件,你可以使用$scope.$watch()如下:
$scope.$watch('model.customer',function (customer) { $scope.setActiveCustomer(customer); });
希望这可以帮助.