2016-07-14 94 views
-1

我有這樣的JSON數據,這是使用ES的match_phrase_prefix查詢從自動完成查詢Elasticsearch搜索結果返回:AngularJS一個foreach迭代

"hits": { 
    "total": 3, 
    "max_score": 3.3071127, 
    "hits": [ 
     { 
     "_source": { 
      "ymme": "bourne supremacy" 
     } 
     }, 
     { 
     "_source": { 
      "ymme": "bourne ultimatum" 
     } 
     }, 
     { 
     "_source": { 
      "ymme": "bourne idendity" 
     } 
     } 

所以我想要做的就是通過這個循環來獲得「ymme」的值。

我在我的控制器的自動完成建議目前的功能是這樣的:

//suggestions 
$scope.getSuggestions = function(query) { 
$scope.isSearching = true; 
return searchService.getSuggestions(query).then(function(es_return){ 
    var phrases = es_return.hits.hits; 
    console.log(phrases); 
    if (phrases) { 
     return $scope.autocomplete.suggestions = phrases; 
     console.log(suggestions) 
    } 
    else { 
     $scope.autocomplete.suggestions = []; 
     $scope.noResults = true; 
    } 
    $scope.isSearching = false; 
    }, 
    function(error) { 
     console.log('ERROR: ', error.message); 
     $scope.isSearching = false; 
    }); 
    }; 

這到底是怎麼發生的是,建議在下拉列表中顯示,但「ymme」的值(實際的建議)是沒有被傳遞給$ scope.autocomplete.suggestions。

我需要遍歷結果並將「ymme」的值推送到$ scope.autocomplete.sugggestions,我一直在尋找angular.forEach,但我不知道如何實現它,因爲我正在處理一個鍵,值對是在一個對象中,這是在一個對象,它是一個數組,這是又一個目的:

"hits": { 
    "hits": [ 
     { 
     "_source": { 
      "ymme": "bourne supremacy" 
      } 
     } 
     ] 
    } 

任何意見,建議?

UPDATE

//suggestions 
$scope.getSuggestions = function(query) { 
    $scope.isSearching = true; 
    return searchService.getSuggestions(query).then(function(es_return) { 
     var phrases = es_return.hits.hits; 
     if (phrases) { 
      return $scope.autocomplete.suggestions = phrases.map(function(item) { 
       return item._source.ymme; 
      }) 
     } else { 
      $scope.autocomplete.suggestions = []; 
      $scope.noResults = true; 
     } 
     $scope.isSearching = false; 
    }, function(error) { 
     console.log('ERROR: ', error.message); 
     $scope.isSearching = false; 
    }); 
}; 

HTML dropwdown

<input type="text" ng-model="searchTerms" class="form-control input-md" placeholder="{{ searchTerms }}" name="q" id="srch-term" uib-typeahead="query as query._source.ymme for query in getSuggestions($viewValue)" typeahead-on-select="search($item)"> 

回答

1

您可以使用map這樣的:

return $scope.autocomplete.suggestions = phrases.map(function(item) { 
    return item._source.ymme; 
}); 

HTML:

<input type="text" 
    ng-model="searchTerms" class="form-control input-md" 
    placeholder="{{ searchTerms }}" name="q" 
    id="srch-term" 
    <!-- change this --> 
    uib-typeahead="query for query in getSuggestions($viewValue)" 
    typeahead-on-select="search($item)"> 

我希望這會幫助你。

+0

感謝您的快速回答,我喜歡這個比的forEach更好,但我將如何實現在$ scope.getSuggestions以上?我是否需要擺脫if else語句或....? – user3125823

+1

只需用回答中的代碼替換'return $ scope.autocomplete.suggestions = phrases;'並且移除'console.log(建議)'它未被使用。不用謝。 –

+0

真棒!我必須稍後再執行此操作,一旦我得到它,我就會接受!非常感謝 – user3125823

0

,如果你想要去角forEach - >

var res = []; 
angular.forEach(hits.hits, function(src) { 
    res.push(src._source.ymme);  
});