繁体   English   中英

类型错误:无法使用 AngularJS 读取未定义的属性“推送”

[英]TypeError: Cannot read property 'push' of undefined with AngularJS

我正在尝试将我的数据推送到此CompareArray但出现以下错误:

TypeError: Cannot read property 'push' of undefined
    at Object.addResult (src\services\CompareService.js:22)

代码:

this.addResult = function (type, result) {
    this[type.toLowerCase() + 'CompareArray'].push(result);
    this.checkToDisplayLinks(type);
    this.checkToDisableCheckboxes(type);
};

在推送任何内容之前检查它是否未定义和初始化

if(this[type.toLowerCase() + 'CompareArray']){
 this[type.toLowerCase() +  'CompareArray'].push(result);
}
else
{
   this[type.toLowerCase() +  'CompareArray'] = [result];
}

如果你仔细观察,错误是自我描述的。

Cannot read property 'push' of undefined at Object.addResult 

addResult 是你的方法。 你在“推”

this[type.toLowerCase() + 'CompareArray'].push(result);

它说 - 未定义的“推送”。 意思是

this[type.toLowerCase() + 'CompareArray'] 

未定义。 Push 是一种仅适用于数组的方法。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM