2013-07-03 57 views
0

我已經在js(developer.js)文件中編寫了這段代碼。爲什麼slice()顯示錯誤,但代碼完美運行?

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) 

附:
當我在項目內共有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" 
} 
+1

顯然你的物品數組的前4項是未定義的... – Christoph

+0

你可以轉儲你的'物品'嗎? – EpokK

+0

1.您傳遞給服務的項目 - 是否最初使用'$ http'獲取?你能寫出調用服務的代碼嗎?如在控制器中是否存在這種情況,如果是這樣,它是如何調用的(以及何時調用它)? - 對我來說,它看起來像服務被調用時,該項目可能尚未填充 - 爲什麼不在服務中添加一個簡單的if條件 - if(angular.isArray(items)){//繼續拼接}' - 它應該可以解決你的問題... – callmekatootie

回答

1

您是否嘗試過處理實際的錯誤?

也許只是試試這個?

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

您可能還想知道爲什麼您的某些數據未定義,可能存在您可能需要處理的問題。

+0

謝謝你 – Photonic

相關問題