Javascript charAt返回时未定义
我有下面的函数,但是它说字符是未定义的。错误与警戒线有关。如果我做了警报(值),它会给我的值没有问题Javascript charAt返回时未定义,javascript,angularjs,Javascript,Angularjs,我有下面的函数,但是它说字符是未定义的。错误与警戒线有关。如果我做了警报(值),它会给我的值没有问题 $scope.markAnswer = function(answerID, questionID) { if ($scope.containsObject(answerID, $scope.selectedAnswer)) { $scope.selectedAnswer.splice($scope.selectedAnswer.indexOf(answe
$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)
TypeError:未定义不是函数
在http://127.0.0.1:9000/modules/core/controllers/home.js:57:25
在Object.forEach(http://127.0.0.1:9000/lib/angular/angular.js:325:18)
在范围内。$Scope.markAnswer(http://127.0.0.1:9000/modules/core/controllers/home.js:56:17)
在http://127.0.0.1:9000/lib/angular/angular.js:10903:21
在http://127.0.0.1:9000/lib/angular-触摸/角度触摸。js:441:9
在范围内。$eval(http://127.0.0.1:9000/lib/angular/angular.js:12811:28)
在范围内。$apply(http://127.0.0.1:9000/lib/angular/angular.js:12909:23)
在HTMLDEVELENT。(http://127.0.0.1:9000/lib/angular-触摸/角度触摸。js:440:13)
在htmldevelment.jQuery.event.dispatch(http://127.0.0.1:9000/lib/jquery/dist/jquery.js:4430:9)
位于htmldevelment.elemData.handle(http://127.0.0.1:9000/lib/jquery/dist/jquery.js:4116:28)
您可以试试这个:
String(value).charAt(0)
我假设发生这种情况是因为您的值不是字符串,而
charAt
是一个返回字符串中指定索引处字符的方法。值是字符串吗?请检查一下。谢谢,这很奇怪,因为即使在对字符串调用charAt()时,它也会对其求值,但在控制台中它仍然会给出此错误。然而,这已经解决了它。