简体   繁体   中英

charAt coming back as undefined

I've got the following function, however it's saying the charAt is undefined. The error is relating to the alert line. If i do alert(value) it gives me the value no problems.

$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");
            }

        });
    }

The following error:

 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)

You could try this one:

String(value).charAt(0)

I suppose that this is happening because your value is not a string and charAt is a method that returns the character at the specified index in a string .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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