Angularjs ui-select下拉选择函数调用问题

Mad*_*u K 19 angularjs angularjs-bootstrap ui-select

在我的AngularJS项目中,我使用正常的选择下拉列表并使用ng-change进行函数调用,这完全正常.现在我想将相同的下拉列表迁移到ui-select.但是on-select函数调用不起作用,我尝试了不同的方法,但没有运气.请找我的plnkr

以下是我用ui-select尝试的两种方式:

<ui-select ng-model="uiSelectedCustomer" theme="select2" on-select="getDataBasedonCustomer(uiSelectedCustomer)" style="min-width: 300px;">
    <ui-select-match placeholder="Select a customer..">{{$select.selected.name}}</ui-select-match>
    <ui-select-choices repeat="cust in customers | filter: $select.search">
        <div ng-bind-html="cust.name | highlight: $select.search"></div>
    </ui-select-choices>
</ui-select>


<ui-select ng-model="uiSelectedCustomer" theme="select2" on-select="getDataBasedonCustomer(uiSelectedCustomer)" style="min-width: 300px;">
    <match placeholder="Select a customer in the list or search his name ">{{$select.selected.name}}</match>
    <choices repeat="cust in customers | filter: $select.search">
        <div ng-bind-html="cust.name | highlight: $select.search"></div>
    </choices>
</ui-select>
Run Code Online (Sandbox Code Playgroud)

Sun*_*ity 56

从您的代码中可以理解,您希望选择项目并根据选择执行某些操作(如果是)?然后看到这个

添加on-select="onSelected($item)"到您ui-select和控制器中:

$scope.onSelected = function (selectedItem) {
  //do selectedItem.PropertyName like selectedItem.Name or selectedItem.Key 
  //whatever property your list has.
}
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,回调函数不会改变任何东西,除非`$ item`作为参数传递并在函数中使用.我相信这是因为`on-select`回调被称为_before_`ng-model`被更新. (2认同)