Javascript TypeError:无法读取属性';推动';用AngularJS表示未定义的
我试图将数据推入此Javascript TypeError:无法读取属性';推动';用AngularJS表示未定义的,javascript,arrays,angularjs,Javascript,Arrays,Angularjs,我试图将数据推入此compararray中,但出现以下错误: 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);
compararray
中,但出现以下错误:
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是一种仅在阵列上可用的方法
希望这有帮助。表示
此[type.toLowerCase()+'CompareArray']
未定义标记用于angular JS(即v1),而标记用于angular JS(即2+),因此只有一个标记可以相关。你能把不正确的移走吗?(我相信代码是AngularJS,所以去掉angular
标记)嗯。。。。。因此,它的编写方式不会添加任何结果,我对其进行了编辑,以便在定义数组时添加值
this[type.toLowerCase() + 'CompareArray']