charAt作为未定义返回 [英] charAt coming back as undefined

查看:166
本文介绍了charAt作为未定义返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下函数,但是说的是charAt是未定义的.该错误与警报行有关.如果我发出警报(值),则它不会给我带来任何问题.

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

        });
    }

以下错误:

 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是一种返回 string 中指定索引处的字符的方法.

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.

这篇关于charAt作为未定义返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆