2016-10-22 26 views
0

我正在創建一個應用程序,用於搜索字典中的某些單詞,如果單詞存在於字典中,則將定義以及其他一些數據輸出到應用程序視圖。我的問題是,只要字典中存在多個單詞,應用程序只輸出找到的最後一個單詞。如何在for循環的每次迭代後向Ionic中的應用程序視圖添加數據?

以下是進行線性搜索的代碼。 (我知道這也許可以使用二進制搜索進行優化,但現在它成爲我的目的:

for (var i=0; i < result.length; i++) { 
    for(var k=0; k <my_dictionary.length; k++){ 
      if(result[i]== my_dictionary[k].word) { 
       me.word = my_dictionary[k].word; 
       me.definition = my_dictionary[k].definition; 
       me.usage = my_dictionary[k].usage; 
       console.log(me.word); 
      } 
    } //looping over words in dictionary 
} 

下面是在模板文件中的代碼,使這些變量:

<h3 class="text-center" ng-show="home_ctrl.word"><u>Word:</u>{{home_ctrl.word}}</h3> 
    <h3 class="text-center" ng-show="home_ctrl.definition"><u>Explanation:</u>{{home_ctrl.definition}}</h3> 
    <h3 class="text-center" ng-show="home_ctrl.usage"><u>Usage:</u>{{home_ctrl.usage}}</h3> 

有一些的方式,因爲它是在字典中的每個字輸出到視圖?

回答

0

你終於可以創建一個empty arraymeObject對象結果推入陣。 ,您可以使用ng-repeat在html中循環數組。

var meArray = []; 

for (var i=0; i < result.length; i++) { 
    for(var k=0; k <my_dictionary.length; k++){ 
     if(result[i]== my_dictionary[k].word) { 
      var meObject = { 
       word: my_dictionary[k].word; 
       definition: my_dictionary[k].definition; 
       usage: my_dictionary[k].usage; 
      } 
      me.push(meObject); 
     } 
    } //looping over words in dictionary 
} 

$scope.home_ctrl_array = meArray; 

<div ng-repeat="home_ctrl in home_ctrl_array"> 
    <h3 class="text-center" ng-show="home_ctrl.word"><u>Word:</u>{{home_ctrl.word}}</h3> 
    <h3 class="text-center" ng-show="home_ctrl.definition"><u>Explanation:</u>{{home_ctrl.definition}}</h3> 
    <h3 class="text-center" ng-show="home_ctrl.usage"><u>Usage:</u>{{home_ctrl.usage}}</h3> 
</div> 
+0

我無法滾動到按下調用啓動全部功能的按鈕。 –

0

如果你知道jquery選擇,你可以選擇你想要顯示的html元素,並做一個.html()或.text()。這將在有匹配的if語句中。

相關問題