Javascript TypeError:无法读取属性';0';在Angularjs中未定义的

Javascript TypeError:无法读取属性';0';在Angularjs中未定义的,javascript,angularjs,Javascript,Angularjs,我看到了很多与此相关的问题 TypeError:无法读取未定义的属性“0”,但它们都无助于解决我的错误 我的要求是onClick按钮(比如:ng click=“addRow(tester.devices)”),我必须调用一个函数- 其中,如果已经存在/正在调用devicelist,则无需定义\u scope.tester.devices=[]否则\u scope.tester.devices=[]以避免错误: 无法读取未定义的属性“unshift” 我的代码如下 _scope.addRow =

我看到了很多与此相关的问题 TypeError:无法读取未定义的属性“0”,但它们都无助于解决我的错误

我的要求是onClick按钮(比如:ng click=“addRow(tester.devices)”),我必须调用一个函数-

其中,如果已经存在/正在调用devicelist,则无需定义
\u scope.tester.devices=[]
否则
\u scope.tester.devices=[]以避免错误:
无法读取未定义的属性“unshift”

我的代码如下

_scope.addRow = function (list) {
        _scope.uniqueId = Math.round((Math.random() * 10) * 10000);
        if (_scope.tester.devices) {
            _scope.tester.devices.unshift({
                'name': '',
                'id': '',
                'uniqueId': _scope.uniqueId,
            });


        } else {
            _scope.tester.devices = [];
            _scope.tester.devices.unshift({
                'name': '',
                'id': '',
                'uniqueId': _scope.uniqueId,
            });
        }
        _scope.modifyField[list[0].uniqueId] = true;
    }
Edit1:我将$scope定义为_scope。所以这不是问题

这个问题解决了

嗨, 您确定您的函数参数(“列表”)是数组吗?它似乎没有定义。

嗨,
您确定您的函数参数(“列表”)是数组吗?它似乎未定义。

将数组传递给函数,似乎将错误的参数传递给函数_scope.addRow()


将数组传递给函数后,它对我来说工作正常。

将数组传递给函数,似乎您将错误的参数传递给了函数\u scope.addRow()


将数组传递给函数后,它对我来说运行良好。

我通过修改
\u范围解决了这个问题。modifyField[list[0]。uniqueId]=true
\u scope.modifyField[\u scope.uniqueId]=true

不幸的是,我无法删除我的问题,因为有人根据堆栈溢出规则为此投入了时间

守则如下:

 _scope.addRow = function (list) {
        _scope.uniqueId = Math.round((Math.random() * 10) * 10000);
        if (_scope.tester.devices) {
            _scope.tester.devices.unshift({
                'name': '',
                'id': '',
                'uniqueId': _scope.uniqueId,
            });
             _scope.modifyField[list[0].uniqueId] = true;

        } else {
            _scope.tester.devices = [];
            _scope.tester.devices.unshift({
                'name': '',
                'id': '',
                'uniqueId': _scope.uniqueId,
            });
        }
        _scope.modifyField[_scope.uniqueId] = true;
    }

我通过修改
\u scope.modifyField[list[0].uniqueId]=true解决了这个问题
\u scope.modifyField[\u scope.uniqueId]=true

不幸的是,我无法删除我的问题,因为有人根据堆栈溢出规则为此投入了时间

守则如下:

 _scope.addRow = function (list) {
        _scope.uniqueId = Math.round((Math.random() * 10) * 10000);
        if (_scope.tester.devices) {
            _scope.tester.devices.unshift({
                'name': '',
                'id': '',
                'uniqueId': _scope.uniqueId,
            });
             _scope.modifyField[list[0].uniqueId] = true;

        } else {
            _scope.tester.devices = [];
            _scope.tester.devices.unshift({
                'name': '',
                'id': '',
                'uniqueId': _scope.uniqueId,
            });
        }
        _scope.modifyField[_scope.uniqueId] = true;
    }

我在控制器函数中将$scope定义为_scope。问题在于_scope.modifyField。我解决了。感谢您的帮助我在控制器函数中将$scope定义为_scope。问题在于_scope.modifyField。我解决了。感谢帮助否,这是函数的参数,未定义。抛出错误是因为您试图像数组一样访问它。现在,如果不再需要它,可以从方法定义中将其删除:
code
\u scope.addRow=function(){
code
否,它是未定义的函数的参数。引发错误的原因是您试图像数组一样访问它。现在,如果不再需要它,可以从方法定义中删除它:
code
\u scope.addRow=function(){
code