2015-09-05 52 views
1

試圖顯示來自與ES詞組建議者的建議對象的選項陣列使用AngularJS第一選項10顯示搜索建議,但難倒....如何從Elasticsearch短語建議者與AngularJS

此輸出第一元素在選項數組中:

{{suggestions.options[0].text}} 

該文本是實際的建議。從開發控制檯

輸出在Chrome:

suggest: {,…} 
phraseSuggestion: [{text: "pytho", offset: 0, length: 5, options: [{text: "python", score: 0.12858662}]}] 
0: {text: "pytho", offset: 0, length: 5, options: [{text: "python", score: 0.12858662}]} 
length: 5 
offset: 0 
options: [{text: "python", score: 0.12858662}] 
0: {text: "python", score: 0.12858662} 
text: "pytho" 

如何訪問:

options: [{text: "python", score: 0.12858662}] 

在我的HTML模板,並顯示數組中的第5至10個選項。

+0

我聽不懂,你可以用{{suggestions.options [0] .text}}來訪問它' –

+0

@Michelem是的我明白,我想要做的是列出5 - 10選項,而不是1,這是所有代碼顯示 – user3125823

回答

1

所以,如果你有例如這樣的JSON輸出,你要顯示的第一個2選擇項目,你可以這樣做:

JSFiddle

HTML:

<div ng-app="myApp" ng-controller="dummy"> 
    <p ng-repeat="option in out.options | limitTo:2">{{option.text}} - {{option.score}}</p> 
</div> 

JS:

angular.module('myApp', []) 
    .controller('dummy', ['$scope', '$sce', function ($scope, $sce) { 
    $scope.out = { 
     "options": [{ 
      "text": "python", 
       "score": 0.12858662 
     }, { 
      "text": "linux", 
       "score": 0.12858662 
     }, { 
      "text": "php", 
       "score": 0.12858662 
     }, { 
      "text": "js", 
       "score": 0.12858662 
     }, { 
      "text": "java", 
       "score": 0.12858662 
     }] 
    }; 

}]); 
+0

非常感謝你!仍在學習Angular - 它看起來很棒 - 知道如何使用它是戰鬥的一半! – user3125823