2015-09-19 24 views
-1

我正在學習AngularJS並嘗試在使用Elasticsearch的簡單搜索應用中實現Angular UI Bootstrap Typeahead。我想使用$ http服務加載異步結果,並沒有與js承諾一起工作。這個javascript的承諾是正確的嗎?

更新:我做了一些更多的學習和測試

我已經包括如事先鍵入的內容在我的應用程序的依賴:

var searchApp = angular.module('searchApp', ['elasticsearch', 'ngSanitize', 'ui.bootstrap']); 

這裏是JS代碼的承諾與ES匹配查詢映射到content.autocomplete字段:

this.getSuggestions = function(query) { 
var deferred = $q.defer(); 

esClient.search({ 
    index: 'bigtestindex', 
    body: { 
    "query": { 
     "match": { 
      "content.autocomplete": { 
      "query": query, 
      "operator": "and" 
      } 
     } 
     }, 
    "suggest": { 
     "text": query, 
     "phraseSuggestion": { 
     "phrase": { 
      "field": "content", 
      "direct_generator": [{ 
      "field": "content", 
      "suggest_mode": "popular", 
      "min_word_length": 3, 
      "prefix_length": 2 
      }] 
     } 
     } 
    }, 
    "size": 5, 
    "_source": ["content.autocomplete"] 
    } 
}).then(function(es_return) { 
    deferred.resolve(es_return); 
}, function(error) { 
    deferred.reject(error); 
}); 

return deferred.promise; 

};

有了這個代碼,我想實現異步效果如圖所示here

下面是相關的HTML顯示可能的匹配:

<li ng-repeat="gram in autocomplete.edgengrams"> 
     <p ng-mousedown="searchForSuggestion()"><small>Search Suggestions: &mdash;</small>{{gram.content}}</p> 
    </li> 
+0

嗨user3125823,我很樂意幫忙,但我不確定你在問什麼。你應該試着將這個將軍看起來是正確的問題解開成更具體和狹隘的東西。 –

+2

我們很想知道您正在嘗試實現哪些功能,以及哪些功能無法按預期工作? – Rayon

+0

你可以直接返回$ http.get的承諾。 – Vincenzo

回答