Angularjs - 声明工厂与一个嵌套数组单个对象 - 获得的ReferenceError:......没有定义 [英] Angularjs - Declaring factory with a single object with a nested array - getting ReferenceError: .... is not defined

查看:273
本文介绍了Angularjs - 声明工厂与一个嵌套数组单个对象 - 获得的ReferenceError:......没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几种不同的方法一个工厂,我想声明的是具有嵌套在一个数组的对象。那一个嵌套数组将是我的应用程序的主要动力,这将有嵌套阵列,以及(还有更多)。不过,我想嵌套在一个对象嵌套数组保持。我试过 myObject的以下,但我不断收到错误是不确定的。

I have a factory with several different methods and I am trying to declare an object that has an array nested in it. That one nested array will be the main driving force for my app and it will have nested arrays as well(many more). However, I do want the nested array to stay nested in that one object. I tried the following but I keep getting error that myObject is is undefined.

app.factory('myService', function() {
    return {
        myobject: {name: "MockName", version: "1", myArray: []},
        addToArray: function(){
            this.myobject.myArray.push({name: "Something + " (typeof(myObject.myArray) === 'undefined' ? 1 : myObject.myArray.length, anotherArrayInside: []})
        },
        .......
    };
});

我仍然在角一个新手,所以我不知道如何解决这个问题。有什么建议?

I am still a novice in Angular so I have no idea how to fix this problem. Any suggestions?

解决

所以我想通了,我没有把code的一件和那件code的引起了我的问题:

So I figured out that I didn't put one piece of code and that piece of code was causing my issue:

这是myObject.myArray,因为myObject.myArray未定义的是,这是给我的错误。因为我已经改变了这一切为的typeof(myObject的)===未定义

it was the myObject.myArray , because myObject.myArray was not defined yet, it was giving me an error. I have since changed that to typeof(myObject) === 'undefined'

推荐答案

这是一个JavaScript的问题。

This is a Javascript problem.

app.factory('myService', function() {
    var myobject = {name: "MockName", version: "1", myArray: []}
    return {
        addToArray: function(){
            myobject.myArray.push({name: "Something" , anotherArrayInside: []})
        },
        .......
    };
});

这篇关于Angularjs - 声明工厂与一个嵌套数组单个对象 - 获得的ReferenceError:......没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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