单击按钮时 AngularJS 搜索

标签 angularjs angularjs-ng-repeat

我有一个表单,我希望用户能够在文本框中输入搜索词,然后单击按钮来搜索列表。是否可以通过 ng-repeat 和单击按钮来执行类似的操作?我对 Angular 还很陌生,我不确定是否可以将它用于此类功能。我有一些代码:

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>

Controller :

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

最佳答案

您可以指定要搜索的字段,例如:

    <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 变量。

关于单击按钮时 AngularJS 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33524710/

相关文章:

javascript - 使用 angularjs 渲染表格

javascript - 如何使用 momentjs 或 vanilla JS 将 ISO 日期格式转换为 yyyy-MM-dd 格式?

javascript - Angular.js 在 DOM 准备好之前显示加载动画

AngularJS: 'Duplicates in a repeater are not allowed. Use ' track by'表达式来指定唯一键。”

javascript - 带开关的 Angular 过滤器列表( Angular Material )?

javascript - 如何跨 android 和 JS bridge 配置通用数据模型?

angularjs - 我如何获取有关谁更改了我在 watch 中设置的变量的信息( Angular )

javascript - 如何在 AngularJS 中创建积分分配系统?

javascript - 使用指令更新 ng-repeat 创建的图像

angularjs - 检测自定义过滤器何时在 AngularJS 中完成