Javascript 解析工厂中声明的数组时,“无法读取未定义的属性‘id’”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28763272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Cannot read property 'id' of undefined" when parsing an array declared in a factory
提问by George Netu
I am building a basic app (with MEAN web frameworks and node webkit) in order to understand angularjs better.
我正在构建一个基本的应用程序(使用 MEAN Web 框架和节点 webkit)以便更好地理解 angularjs。
Here is the content of my notificationFactory.js
这是我的内容 notificationFactory.js
function notificationFactory() {
var fooMessages = [
{
id: 4,
dismissable: true,
name: "fooooBaaar",
function: '',
showInTopBar: false,
priority: "high",
icon: 'fooIconBarBarBar',
topBarIcon: 'fooIconIconIcon'
},
{
id: 3,
dismissable: true,
name: "foofooBarBar",
function: '',
showInTopBar: false,
priority: "high",
icon: 'fooIconfooIcon',
topBarIcon: 'IconIconIcon'
},
{
id: 2,
dismissable: true,
name: "foo foo",
function: '',
showInTopBar: false,
priority: "high",
icon: 'fooBaaaaaar',
topBarIcon: 'IconFooIcon'
},
{
id: 1,
dismissable: true,
name: "foo",
function: '',
showInTopBar: false,
priority: "high",
icon: 'fooIcon',
topBarIcon: 'fooIconIcon'
},
]
fooMessages.TopBarDismiss = function (message) {
$.each(fooMessages, function (i, v) {
if(v.id = message.id) {
fooMessages.splice(i,1);
}
});
}
return fooMessages;
}
angular.module('fooDemo').factory('notificationFactory', notificationFactory);
I call the TopBarDismiss()function in an HTML template using:
我TopBarDismiss()使用以下方法在 HTML 模板中调用该函数:
<div class="fooDismiss" ng-click="notificationFactory.TopBarDismiss(message)">Dismiss</div>
When I check out the console after pressing my Dismiss "button", I get this:
当我按下“关闭”“按钮”后查看控制台时,我得到以下信息:
TypeError: Cannot read property 'id' of undefined
at notificationFactory.js:94
at Function.m.extend.each (jquery.min.js:2)
at Array.fooMessages.TopBarDismiss (notificationFactory.js:93)
at fb.functionCall (angular.js:10847)
at angular-touch.js:472
at k.$get.k.$eval (angular.js:12702)
at k.$get.k.$apply (angular.js:12800)
at HTMLDivElement.<anonymous> (angular-touch.js:471)
at angular.js:3097
at r (angular.js:325)angular.js:10072 (anonymous function)angular.js:7364 $getangular.js:12802 $get.k.$applyangular-touch.js:471 (anonymous function)angular.js:3097 (anonymous function)angular.js:325 rangular.js:3096 r.triggerHandlerangular.js:3111 S.(anonymous function)angular-touch.js:453 (anonymous function)angular.js:2853 (anonymous function)angular.js:325 rangular.js:2852 c
so it must be the
所以它必须是
$.each(fooMessages, function (i, v) {
if (v.id == message.id) {
}
});
part that is quite horrible.
相当可怕的部分。
Can you, guys, spot the error for me, please?
伙计们,你们能帮我找出错误吗?
回答by Omri Aharon
First, as said in the comments, you need to make sure the messageobject passed is really the one you're looking for.
首先,正如评论中所说,您需要确保message传递的对象确实是您要查找的对象。
Then, if you just want to splice you can do:
然后,如果你只是想拼接,你可以这样做:
fooMessages.TopBarDismiss = function (message) {
var index;
for (var i = 0; i < fooMessages.length; i++) {
if(fooMessages[i].id == message.id) {
index = i;
break;
}
}
if (index) {
fooMessages.splice(index,1);
}
}
回答by marcieng
I think you need to pass messageto the inner function. In that scope, messageis undefined.
我认为你需要传递message给内部函数。在那个范围内,message是undefined。

