2015-11-14 26 views
0

我使用elasticsearch存儲標籤,並且想讓ngTagsInput爲自動完成動態獲取它們。它的工作原理上到哪裏ngTagsInput接收來自elasticsearch一個答案,在這裏我收到以下錯誤在瀏覽器控制檯點:讓ngTagsInput與elasticsearch的答案一起工作

Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 
'track by' expression to specify unique keys. Repeater: item in 
suggestionList.items track by track(item), Duplicate key: undefined, 
Duplicate value:{"_index":"data","_type":"tags","_id":"4","_score":1,"_source":{"id":4,"text":"Testinchen"}} 

這裏的問題是,我收到的承諾中包含有4個元素的數組(如果我有更多的聲譽,我會張貼一張圖片在這裏,但沒有,堆棧溢出不會讓我:x)

我已經嘗試了整夜獲得ngTagsInput接受這個承諾,我設法做的最好的是以某種方式檢索我的對象如果彈性搜索中只有一個命中而不是多個。

的HTML代碼看起來如下:

<div data-ng-controller="TagsController"> 
    <tags-input ng-model="taglist"> 
     <auto-complete source="loadTaglist($query)"template="tagTemplate"></auto-complete> 
    </tags-input> 
</div> 
<script type="text/ng-template" id="tagTemplate"> 
    <span ng-bind-html="$highlight($getDisplayText())"></span> 
    <div ng-repeat="model in data._source"> 
     @{{model}} 
    </div> 
</script> 

在前面的@了{{模型}}這裏是因爲這個運行在後臺laravel,所以從來不介意^^

而且該JS如下:

petopia.controller('TagsController', function($scope, tags, esClient) { 

    $scope.taglist = [{ 
     "id" : "1", 
     "text": "Pet" 
    }]; 

    $scope.loadTaglist = function(query) { 
     var taglist = esClient.search({ 
      q: "*"+query+"*" 
     }).then(function (response) { 
      return response.hits.hits; 
     }, function (error) { 
      console.trace(error.message); 
     }); 

     return tags.load(taglist); 
    }; 
}); 

petopia.service('tags', function($q) { 
    this.load = function(taglist) { 

     var deferred = $q.defer(); 
     deferred.resolve(taglist); 
     console.log(deferred.promise); 
     return deferred.promise; 
    }; 
}); 

任何幫助,將不勝感激,因爲我在這裏很茫然的人誰是很新的angularjs。

回答

0

當你的數組有重複時,你應該通過索引來跟蹤它。

ng-repeat="model in data._source track by $index"

+0

是的,我試過了,但即使是之前的數據獲取到模板的問題出現了。如果elasticsearch僅發送一個命中,那麼它就有效,除了包含id和text兩個字符串的模型。但是,謝謝你的快速回復! – spec1alk