2015-11-04 144 views
0

嗨我有一个表单,可以搜索数据输入到文本框,然后单击提交按钮。它在第一次完美运行,但是当我尝试清除这些值并进行第二次搜索时,它会开始显示数据而无需单击按钮。它只需在文本框中输入即可进行双向绑定。我需要它只在按钮被点击时才工作。这里是我的代码:AngularJS搜索按钮

HTML

<form name="form" id="form" novalidate> 
<div> 
    <input ng-model="searchModel.address" /> 
    <input ng-model="searchModel.city" /> 
    <button type="submit" ng-click="filterSearch = searchModel">Search</button> 
    <button type="reset">Reset</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> 
</div> 

控制器:

(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" 
       } 

    ]; 

} 

vm.SearchModel = function() { 
    var vm = this; 
    vm.filterSearch = angular.copy(searchModel); 
}; 
})(); 
+0

你'NG-click'只是做一个参考分配。你的意思是做一些像'ng-click =“SearchModel();”'这实际上是深层复制? – ryanyuyu

+0

@ryanyuyu当我这样做时,搜索按钮根本不起作用。 – hollyquinn

回答

0

我终于想通了这一点。

HTML

<div class="modal fade" id="myModal" role="dialog"> 
<div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header">Search</div> 
     <div class="modal-body"> 
      <input id="address" ng-model="vm.searchModel.address" /> 
      <input id="city" ng-model="vm.searchModel.city" />    
      <button type="submit" ng-click="vm.search()" data-dismiss="modal">Search</button> 
      <button type="reset">Reset</button> 
     </div> 
    </div> 
</div> 

<div> 
<table> 
    <thead> 
     <tr> 
      <th> 
       Address 
      </th> 
      <th> 
       City 
      </th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr ng-repeat="search in vm.filteredSearch"> 
      <td> 
       {{ search.address }} 
      </td> 
      <td> 
       {{ search.city }} 
      </td> 

     </tr> 
    </tbody> 
</table> 

控制器:

(function() { 
'use strict' 
angular 
    .module('crm.ma') 
    .controller('AdvancedSearchCtrl', function ($scope, $filter) { 

     var vm = this; 

     vm.searches = [ 
        { 
         "address": "202 This St", 
         "city": "Columbus" 

        }, 
        { 
         "address": "205 That St", 
         "city": "Erlanger"       
        } 

     ]; 
     vm.filteredSearch = vm.searches 
     vm.searchModel = {}; 
     vm.search = function() { 
      vm.filteredSearch = $filter('filter')(vm.searches, { 'address': vm.searchModel.address, 'city': vm.searchModel.city }); 
     } 
    }); 

})();