0

我已经在 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) 

PS
当我在项目中总共有 6 个值时,此错误将首先显示 4 次。
items 有对象数组。

当我尝试在过滤器中控制项目时,前 4 次显示“未定义”,然后最后两次显示,

[Object, Object, Object, Object, Object, Object]

在该对象内部,定义了值

items 保存这个对象数组[数据以 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"
}
4

1 回答 1

2

您是否尝试过处理实际错误?

也许只是试试这个?

app.filter('reverse', function() {
  return function(items) {
    if( !items ){ return; } // This will take care of the error
    return items.slice().reverse();
  };
});

您可能还想知道为什么您的某些数据未定义,可能存在您可能需要处理的问题。

于 2015-02-03T17:08:25.470 回答