未捕获的TypeError:value.CommentTypes.join不是Angular函数

问题描述 投票:0回答:1

在angularjs中,我使用一种方法调用不同的方式,一旦它以数组形式获取值,而从另一个地方获取简单值而不是数组。

我的方法是:

function fillReviewObject() {
    angular.forEach(vm.surveyQuestion, function (value, key) {
        value.CommentTypes = value.CommentTypes.join(',');
    });
}

值为Object我从“Value”对象中获取“CommentType”的值。当我以数组格式传递值时,

CommentTypes Array(1)[2] 

注释类型长度为1的值为“2”。它在我的代码中工作正常。

但是当我以这种方式传递Value对象时,

CommentTypes = 2

这里它不是数组格式并给我类型错误。

希望你的建议!

arrays angularjs angular model-view-controller typeerror
1个回答
1
投票

您只能在数组上使用join方法,否则该函数不存在。在使用join函数之前尝试检查值是否为数组,可能是这样的:value.CommentTypes = Array.isArray(value.CommentTypes) ? value.CommentTypes.join(',') : value.CommentTypes;

© www.soinside.com 2019 - 2024. All rights reserved.