[英]Angular Typeahead where model as an Id and Text as a name of the array object
我正在使用Angular UI Typeahead指令,所以你能告訴我如何設置一個model
作為id
並在其上display text
作為name
。我嘗試如下所示,但是它不起作用,該怎么辦? 謝謝。
注意:請在下面的代碼筆上輸入a
或k
進行測試。
JS
var states=[{'id':1,'name':'Alabama'},{'id':2,'name':'Kansas'}]
HTML
<input name="states" id="states" type="text" placeholder="enter a state" ng-
model="selected" typeahead="a.id as a.name for a in states | filter:$viewValue"
class="form-control">
問題是,當您嘗試僅將選定狀態的id
放入ng-model
,它將僅在該輸入字段中顯示選定狀態的id
,因為ng-model
僅具有id
值。
因此,基本上,您必須選擇整個對象,而不僅僅是將id放在ng-model
。 通過它選擇任何狀態時a as a.name
表達式將僅顯示其name
。 而且,只要您想傳遞狀態id
就可以使用selected.id
。
標記
<div class="form-group">
<label for="states">Search for US States</label>
<pre>Model: {{selected | json}}</pre>
<input name="states" id="states" type="text"
placeholder="enter a state" ng-model="selected"
typeahead="a as a.name for a in states | filter:$viewValue"
class="form-control">
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.