我已在js(developer.js)文件中编写此代码。
app.filter('reverse', function() {
return function(items) {
return items.slice().reverse();
};
});
现在,这个代码我放了因为我需要反转ng-repeat结果,所以我创建了手动过滤器。
但是这段代码工作正常,但在控制台中显示错误。
我不想看到我的控制台出现所有错误。
错误:
TypeError: Cannot call method 'slice' of undefined
at Object.<anonymous> (http://192.168.0.14/test/js/developer.js:35:24)
at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:69:187)
at Ia.| (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:129:335)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:68:341
at Ia.| (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:129:340)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:68:341
at Object.e.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:88:347)
at Object.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:147:244)
at Object.e.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:86:286)
at Object.e.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js:88:506)
P.S。
如果项目内总共有6个值,则此错误将首先显示4次。
项目有对象数组。
当我尝试控制过滤器中的项目时,前4次显示“未定义”,然后显示最后两次,显示
[Object, Object, Object, Object, Object, Object]
在该对象内部,定义了值
项目持有这个对象数组[数据以json形式出现]。
{
id: "51c2824088358f0d39000000",
name: "clientdemo",
key: ***,
secret: ***,
description: "It is good.",
icon_url: "/oauth2/static/media/images/icon/default.png"
},
{
id: "51c2cca588358f35aa000003",
name: "grantallllla",
key: ***,
secret: ***,
description: "This asdf",
icon_url: "/oauth2/static/media/images/icon/default.png"
},
{
id: "51d14e8c88358f6e96000024",
name: "12341234",
key: ***,
secret: ***,
description: "Enter descript12341234ion here",
icon_url: "/oauth2/static/media/images/icon/default.png"
},
{
id: "51d14e9888358f6e96000026",
name: "sdfgds345",
key: ***,
secret: ***,
description: "dsfasdfasdfasdf",
icon_url: "/oauth2/static/media/images/icon/default.png"
},
{
id: "51d1758888358f5171000002",
name: "1321",
key: ***,
secret: ***,
description: "zxcvzxcvcxvbxcvb",
icon_url: "/oauth2/static/media/images/icon/51c2819e88358f0d2a000000_1372701408_RhLW.jpg"
},
{
id: "51d3d8c588358f461a000002",
name: "sdafasdf",
key: ***,
secret: ***,
description: "asdfasdf",
icon_url: "/oauth2/static/media/images/icon/51c2819e88358f0d2a000000_1372857885_PSzB.jpg"
}
答案 0 :(得分:2)
您是否尝试过处理实际错误?
也许只是尝试一下?
app.filter('reverse', function() {
return function(items) {
if( !items ){ return; } // This will take care of the error
return items.slice().reverse();
};
});
您可能还想知道为什么您的某些数据未定义,可能存在您可能需要处理的问题。