angularjs - 向 ui.select angular 上的 Controller 发送数据

标签 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>

在我的 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/

相关文章:

javascript - 更改元素值时调用的更改事件 IE 11/Angular

javascript - 使用 github API 在点击时显示 repo 提交

javascript - Angular.copy - 需要更多说明

angularjs - 如何多次使用 $compile 服务?

javascript - ui select2 选定的值不会改变 - angularjs

javascript - 选择 2 个带有 knockout 的延迟加载数据

javascript - 对 JavaScript 函数进行非正则表达式重构的最快方法

angularjs:从指令更​​新 $rootScope 变量

javascript - 具有插值的 ng 样式不渲染背景图像?

javascript - AngularJS 和 UI-Select multiple with data