2015-01-21 68 views
3

我有一些問題了解AngularUI Bootstrap的Typeahead指令。在它們的statesWithFlags對象數組的示例中,它們不一定解釋指令表達式在說什麼。Typeahead對象數組?

state as state.name for state in statesWithFlags | filter:{name:$viewValue} 

鏈接http://angular-ui.github.io/bootstrap/#/typeahead

他們利用國家的兩倍,可能有人給我講解一下?以及解釋過濾器究竟是什麼意思?

例如,當我嘗試用對象數組創建一個對象並使用Typeahead查看數據時,我似乎無法訪問任何數據。

JSON

$scope.dataExample = { 
    'students' : [ 
    { 
     'id': 1, 
     'name': 'John Doe' 
    }, 
    { 
     'id': 2, 
     'name': 'Jane Doe' 
    } 
    ] 
}; 

HTML

<input type="text" ng-model="selectedStudent" typeahead="student as students.name for student in dataExample | filter:{name:$viewValue}"> 

回答

11

此預輸入表達式是相同的語法上ngSelect(在這個博客上的詳細信息:http://www.undefinednull.com/2014/08/11/a-brief-walk-through-of-the-ng-options-in-angularjs/)ngOptions。

它本質上是列表理解。你提供了一個列表,預輸入用來填充選項,目標設置模型值選擇時,以及應如何顯示的選項,語法如下:

modelValue as display for iterationItem in list 

你的問題是, 「在dataExample」部分需要一個數組,但是你給它的對象(你可以給它一個對象,但這不是你想要做的)。你想:

<input type="text" ng-model="selectedStudent" typeahead="student as student.name for student in dataExample.students | filter:{name:$viewValue}"> 

至於你的其他問題,該過濾器只是過濾什麼選項鍵入應顯示。 $ viewValue是一個屬性typeahead設置,無論用戶輸入什麼,過濾器將只選擇匹配該子字符串的選項。

+0

謝謝,這個竅門,並感謝您的信息! – 2015-01-28 14:34:14