我在Angularjs中有一个过滤器,它返回"TypeError: Cannot read property
I have a filter in Angularjs and it returned "TypeError: Cannot read property 'id' of undefined"
这是我的过滤器:
angular.module('App.filters', []).filter('categoryFilter', [function () {
return function (clients, selectedCategory) {
if (!angular.isUndefined(clients) && !angular.isUndefined(selectedCategory) && selectedCategory.length > 0) {
var tempClients = [];
angular.forEach(selectedCategory, function (id) {
angular.forEach(clients, function (client) {
if (angular.equals(client.category.id, id)) {
tempClients.push(client);
}
});
});
return tempClients;
} else {
return clients;
}
};
}]);
在控制器中我有一个函数:
$scope.setSelectedClient = function () {
var id = this.category.id;
if (_.contains($scope.selectedCategory, id)) {
$scope.selectedCategory = _.without($scope.selectedCategory, id);
} else {
$scope.selectedCategory.push(id);
}
return false;
};
和json:
[
{
"id":"1",
"name": "client1",
"image":"images/client1.jpg",
"category": {
"id": "2",
"designation": "Web"
}
},
{
"id":"2",
"name": "client2",
"image":"images/client2.jpg",
"category": {
"id":"1",
"designation": "design"
}
}]
视图:
<ul class="clients-images col-md-12 col-lg-12">
<li class="col-md-3 col-lg-3" data-ng-repeat="client in filtered = (clients | categoryFilter:selectedCategory) | offset: clientsPerPage*currentPage | limitTo: clientsPerPage">
<a href="#projects/{{client.id}}"><img ng-src="{{client.image}}" width="210px" />
<p>{{client.category.designation}}</p></a>
</li>
</ul>
错误出现在这行"if (angular.equals(client.category. net) "Id, Id)){"。如果我调用{{client.category。
我想这会很有帮助:
在过滤器:angular.module('App.filters', []).filter('categoryFilter', [function () {
return function (clients, args) {
if (!angular.isUndefined(clients) && !angular.isUndefined(args.selectedCategory) && args.selectedCategory.length > 0) {
var tempClients = [];
angular.forEach(args.selectedCategory, function (id) {
angular.forEach(clients, function (client) {
if (angular.equals(client.category.id, id)) {
tempClients.push(client);
}
});
});
return tempClients;
} else {
return clients;
}
};
}]);
HTML: <li class="col-md-3 col-lg-3"
data-ng-repeat="client in clients
| categoryFilter:{selectedCategory: selectedCategory}
| offset: clientsPerPage*currentPage
| limitTo: clientsPerPage">
相关文章:
- TypeError:无法读取属性'推'未定义的JavaScript
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- "TypeError:t.start未定义”;,在fullcalendar.min.js中,同时在我的网站上使
- 得到"TypeError:对象不是函数“”;在forEach循环中使用超级测试/超级代理时
- Backbone.View渲染错误"TypeError:无效'instanceof'操作数e.
- "TypeError:无法调用方法'匹配'未定义的“;Angular JS指令中的Morris
- TypeError:undefined不是一个对象(正在评估'msg.innerHTML="Uploa
- backbone.js-集合视图给出"TypeError:无法读取属性'el'未定义的“;错误
- jQuery插件抛出"TypeError”;在noConflict模式之外
- new Parse.View and get"TypeError:undefined不是函数;
- "TypeError:无法读取属性'应用'“未定义”;
- TypeError:无法设置属性"字符串”;的字符串(Javascript)中未定义
- "TypeError:$(..).dialog不是函数“”;当我尝试使用markicon方法打开地图时显示
- "TypeError:Math.Floor不是HTMLButtonElement.onclick中加载的函数(
- "TypeError:obj.indexOf不是函数“”;编写selenium测试脚本时出错
- 添加“;b服务器端":数据表中的true给出错误TypeError:g为null
- "TypeError:这个_subscribe不是函数“;在简单节点模块中尝试RxJS时
- "TypeError:无法调用方法'投掷'未定义的“;单元内测试
- Express.js和connect-mongo-session"TypeError:无法读取属性'混