2015-02-12 26 views
15

在我的angularjs projetc中,我使用正常的選擇下拉菜單,並使用ng-change進行函數調用,該函數完美地工作正常。現在我想要將相同的下拉列表遷移到ui-select。但是,選擇函數調用不起作用,我嘗試了不同的方式,但沒有運氣。請找我plnkrAngularjs ui-select下拉選擇函數調用問題

以下是兩種方法我試過UI的選擇:

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

回答

41

的是從你的代碼明白,要獲得該項目選擇和以此爲基礎進行選擇的東西,如果所以?然後看到這個

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

$scope.onSelected = function (selectedItem) { 
    //do selectedItem.PropertyName like selectedItem.Name or selectedItem.Key 
    //whatever property your list has. 
} 
+2

對於我來說,除非'$ item'被作爲參數傳遞,並在函數中使用的回調函數不會改變任何事情。我相信這是因爲'on-select'回調在'ng-model'被更新之前調用。 – maurice 2015-05-29 17:26:54

+1

這對於多重選擇是有效的,但是對於我來說,在正常選擇的情況下,我必須使用$ select.selected參數調用on-select函數。 – 2015-10-12 15:11:46