2017-01-02 59 views
3

我想找到一種方法來清除代碼的ui選擇選擇。如何以編程方式清除ui-select?

例如我有以下代碼

<ui-select ng-model="selectedCustomerCode" ng-if="CustomerIds.length>1" on-select="CustomerCodeFiltersOnSelectCallback($item, $model)" theme="bootstrap"> 
    <ui-select-match class="ui-select-match" allow-clear="true" placeholder="Επιλογή κωδικού πελάτη...">{{$select.selected.CCode}}</ui-select-match> 
    <ui-select-choices class="ui-select-choices" repeat="customer in CustomerIds | propsFilter: {CCode: $select.search}"> 
    <div ng-bind-html="customer.CCode | highlight: $select.search"></div> 
    </ui-select-choices> 
</ui-select> 

欲碰杯上清除按鈕並清除一些輸入,包括UI的選擇。 清除ui-select的代碼是什麼?

回答

2

,應清除cont.selectedCustomerCode變量:

<button ng-click="cont.selectedCustomerCode = ''">clear</button> 

編輯:

你應該考慮暴露你的變量屬性。這是推薦的方式。

+0

這是有道理的,但我已經嘗試過,並沒有工作。 奇怪的是我把一個

{{selectedCustomerCode}}
在我的HTML和它沒有顯示任何選擇之前或之後的ui-select – vagelis

+0

@vagelis我做了編輯。你可以試試我的建議,並告訴我它是否有效? –

+1

@AbdellahAlaoui你的建議可以解決這個問題。 – Kof

1

有一個選項允許清晰的用戶界面,選擇匹配,做的東西給你,你將有右邊的X,你可以通過點擊它清除。 https://github.com/angular-ui/ui-select/wiki/ui-select-match

<ui-select-match allow-clear="true" placeholder="Select or search in a list..."> 
    <span>{{$select.selected.name}}</span> 
</ui-select-match> 

工作示例http://plnkr.co/edit/DbbUE68QlNLjx97pBZ56?p=preview

+0

這可能是一個解決方案,但我想通過代碼從另一個按鈕用於清除,我在表格中的所有HTML輸入做到這一點。 – vagelis

+0

然後點擊按鈕清除ng模型,例如$ scope.selectedCustomerCode =''或{}或[]。不管它是什麼 –

+0

我已經嘗試過所有這些,但似乎沒有任何工作。 奇怪的是,我把

{{selectedCustomerCode}}
在我的HTML,它說明不了什麼之前或UI選 – vagelis

1

問題已解決。 我不知道爲什麼,但只有ng-model中的selectedCustomerCode無法正常工作。 我的推杆的selectedCustomerCode對象內

ng-model="selectedCustomer.selectedCustomerCode" 

,然後我清除它像這樣

selectedCustomer.selectedCustomerCode = '' 

如果我有

ng-model="selectedCustomerCode" 

清晰的

selectedCustomerCode = '' 

d ID不行

相關問題