2015-11-04 203 views
1

我有一個表單,我希望用戶能夠以搜索條件輸入到文本框中,然後單擊按鈕以搜索列表。用ng-repeat和點擊按鈕可以做到這樣嗎?我對Angular非常陌生,我不確定我是否可以將它用於這種類型的功能。我的代碼一點點:AngularJS搜索按鈕點擊

HTML

<div>  
<input ng-model="filterSearch.address" /> 
<input ng-model="filterSearch.city" /> 
<button ng-click="">Search</button>  
<table> 
    <thead> 
     <tr> 
      <th> 
       Address 
      </th> 
      <th> 
       City 
      </th> 
     </tr>    
    </thead> 
    <tbody>    
     <tr ng-repeat="search in vm.searches | filter: filterSearch"> 
      <td> 
       {{ search.address }} 
      </td> 
      <td> 
       {{ search.city }} 
      </td> 
     </tr> 
    </tbody> 
</table> 

控制器:

(function() { 
'use strict' 
angular 
    .module('crm.ma') 
    .controller('AdvancedSearchCtrl', AdvancedSearchCtrl); 

function AdvancedSearchCtrl() { 
    var vm = this; 
    vm.searches = [ 
       { 
        "address": "202 This St", 
        "city": "Columbus" 
       }, 
       { 
        "address": "205 That St", 
        "city": "Dayton" 
       } 

    ]; 


} 
})(); 

如果任何人都可以點我這將是偉大正確的方向。謝謝。

更新代碼:

<div>  
<input ng-model="searchModel.address" /> 
<input ng-model="searchModel.city" /> 
<button ng-click="filterSearch = searchModel">Search</button>  
<table> 
    <thead> 
     <tr> 
      <th> 
       Address 
      </th> 
      <th> 
       City 
      </th> 
     </tr>    
    </thead> 
    <tbody>    
     <tr ng-repeat="search in vm.searches | filter:{'address': filterSearch.address, 'city': filterSearch.city}"> 
      <td> 
       {{ search.address }} 
      </td> 
      <td> 
       {{ search.city }} 
      </td> 
     </tr> 
    </tbody> 
</table> 

回答

1

您可以指定字段進行搜索像:

<tr ng-repeat="search in vm.searches | filter:{'address': filterSearch.address, 'city': filterSearch.city"> 
     <td> 
      {{ search.address }} 
     </td> 
     <td> 
      {{ search.city }} 
     </td> 
    </tr> 

如果你需要上點擊觸發搜索,你可以使用類似的東西:

ng-click="filterSearch = searchModel" 

並更改您的輸入以使用searchModel變量。

+0

好的。我在我的問題中更新了我的代碼,以反映您提出的更改。雖然我做錯了什麼,因爲當我在文本框中鍵入它時仍然在搜索列表,而不是在單擊按鈕時搜索和顯示。謝謝你的幫助。 – hollyquinn

+0

你能告訴我一些代碼嗎?我不知道如何做到這一點? – hollyquinn

+0

如果只是在第一次點擊後才發生,那是因爲filterSearch對象是通過引用searchModel對象來設置的,而不是被複制的值。只要做'filterSearch = angular.copy(searchModel)' –