在ui.select angular上将数据发送到控制器

Gay*_*yan 3 angularjs angularjs-directive ui-select2

我正在尝试从下拉列表中选择一个值,并从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>
Run Code Online (Sandbox Code Playgroud)

在我的控制器上我得到了客户方法

$scope.setActiveCustomer = function(customer) {
  customersService.getCustomers(customer).then(function(response) {
    $scope.selectedCustomer = response;
  });
};
Run Code Online (Sandbox Code Playgroud)

我的问题是

  • 一旦我选择客户哪个事件我应该用于fire setActiveCustomer(如onchange或其他东西).当我使用ng-click 事件多次激发因为ng-click使用控制激活.

  • 另一件事是我如何设置ui-select的初始值?

谢谢

run*_*arm 7

根据ui-select常见问题解答,您的ng-model表达式应该包含.在其中,例如:

<ui-select ng-model="model.customer" theme="selectize">
Run Code Online (Sandbox Code Playgroud)

要设置初始值,您只需设置ng-model在控制器中指定的属性:

$scope.model = {
  customer: $scope.customers[0] // set the initial selected option
};
Run Code Online (Sandbox Code Playgroud)

对于onchange事件,您可以这样使用$scope.$watch():

$scope.$watch('model.customer', function (customer) {
  $scope.setActiveCustomer(customer);
});
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.