繁体   English   中英

charAt返回未定义

[英]charAt coming back as undefined

我有以下函数,但是说的是charAt是未定义的。 该错误与警报行有关。 如果我做警报(值),它给我的价值没有问题。

$scope.markAnswer = function(answerID, questionID) {
        if ($scope.containsObject(answerID, $scope.selectedAnswer)) {
            $scope.selectedAnswer.splice($scope.selectedAnswer.indexOf(answerID), 1);
        } else {
            $scope.selectedAnswer.push(answerID);
        }

        angular.forEach($scope.selectedAnswer, function(value, key) {
            alert(value.charAt(0));
            if(questionID == res){
                $log.info("questionID");
            }

        });
    }

出现以下错误:

 TypeError: undefined is not a function
    at http://127.0.0.1:9000/modules/core/controllers/home.js:57:25
    at Object.forEach (http://127.0.0.1:9000/lib/angular/angular.js:325:18)
    at Scope.$scope.markAnswer (http://127.0.0.1:9000/modules/core/controllers/home.js:56:17)
    at http://127.0.0.1:9000/lib/angular/angular.js:10903:21
    at http://127.0.0.1:9000/lib/angular-touch/angular-touch.js:441:9
    at Scope.$eval (http://127.0.0.1:9000/lib/angular/angular.js:12811:28)
    at Scope.$apply (http://127.0.0.1:9000/lib/angular/angular.js:12909:23)
    at HTMLDivElement.<anonymous> (http://127.0.0.1:9000/lib/angular-touch/angular-touch.js:440:13)
    at HTMLDivElement.jQuery.event.dispatch (http://127.0.0.1:9000/lib/jquery/dist/jquery.js:4430:9)
    at HTMLDivElement.elemData.handle (http://127.0.0.1:9000/lib/jquery/dist/jquery.js:4116:28)

您可以尝试以下方法:

String(value).charAt(0)

我想发生这种情况是因为您的值不是字符串,而charAt是一种返回字符串中指定索引处的字符的方法

暂无
暂无

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

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