“无法读取未定义的属性'id'解析工厂中声明的数组时 [英] "Cannot read property 'id' of undefined" when parsing an array declared in a factory

查看:113
本文介绍了“无法读取未定义的属性'id'解析工厂中声明的数组时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个基本应用程序(使用MEAN Web框架和节点webkit),以便更好地理解angularjs。

I am building a basic app (with MEAN web frameworks and node webkit) in order to understand angularjs better.

这是我的<$ c $的内容c> 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);

我调用 TopBarDismiss()函数HTML模板使用:

I call the TopBarDismiss() function in an HTML template using:

<div class="fooDismiss" ng-click="notificationFactory.TopBarDismiss(message)">Dismiss</div>

当我按下我的Dismiss按钮后退出控制台时,我明白了:

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

因此它必须是

$.each(fooMessages, function (i, v) {
    if (v.id == message.id) {
    } 
});

部分非常可怕。

伙计们,请你为我发现错误吗?

Can you, guys, spot the error for me, please?

推荐答案

首先,正如评论中所说,你需要制作确定传递的消息对象真的是你要找的对象。

First, as said in the comments, you need to make sure the message object passed is really the one you're looking for.

然后,如果你只是想你可以做的拼接:

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);
        } 

}

这篇关于“无法读取未定义的属性'id'解析工厂中声明的数组时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆