我有从$ scope填充的表和我用来过滤用户名和电子邮件的标题中的两个搜索文本框:
<table class="table">
<thead>
<tr>
<th></th>
<th><input type="search" placeholder="Search" ng-model="searchName"></th>
<th><input type="search" placeholder="Search" ng-model="searchMail"> </th>
<th></th>
</tr>
<tr>
<th>ID</th>
<th>Name</th>
<th>email</th>
<th>password</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="q in dataForTable | filter:{user_fullname : searchName, user_email : searchMail}">
<td>{{q.user_id}}</td>
<td>{{q.user_fullname}}</td>
<td>{{q.user_email}}</td>
<td>{{q.user_password}}</td>
</tr>
</tbody>
dataForTable来自控制器,通过$ http:
$http.get('http://tbrzica.comli.com/rest/index.php/simple/users').success(function(data){
$scope.dataForTable=data;
});
但是当页面最初加载时,表格为空。只有当我在文本框中写入内容并删除输入时,才会填充表格,并且两个条件的搜索都正常工作。这是某种错误吗? 提前谢谢。
编辑:找到解决方案:
我需要在控制器内初始化两个ng-model参数:
$scope.searchName="";
$scope.searchhMail="";
tymeJV,谢谢你的回答。
答案 0 :(得分:3)
在控制器中,您可以为过滤器创建范围函数,如下所示:
$scope.filterUser = function(fullName, email) {
return function(user) {
return (fullName == undefined || fullName.length == 0 || user.user_fullname == fullName)
&& (email == undefined || email.length == 0 || user.user_email == email);
}
}
在HTML中,您可以应用以下过滤器:
<tr ng-repeat="q in dataForTable | filter:filterUser(searchName, searchMail)">
<td>{{q.user_id}}</td>
<td>{{q.user_fullname}}</td>
<td>{{q.user_email}}</td>
<td>{{q.user_password}}</td>
</tr>
希望这有帮助!