Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:无法读取属性';推动';用AngularJS表示未定义的_Javascript_Arrays_Angularjs - Fatal编程技术网

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']