Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I try to make an angularjs project. I have the following code:

HTML:

<ul>
    <li ng-repeat="answer in question.answers">
        <button type="button" ng-click="answer($index)">{{answer}} ({{$index}})</button>
    </li>
</ul>

Controller:

.controller('QuestionCtrl', ['$scope', 'Questions', function($scope, Questions) {
    $scope.question = Questions.query();

    $scope.answer = function(ans) {
        console.log('clicked' + ans);
    }
}]);

The list is generated as expected, but when I click on on of the elements, I get this error message:

Error: fnPtr is not a function Parser.prototype.functionCall/<@http://localhost:8000/app2/lib/angular/angular.js:10169 ngEventDirectives[directiveName]</<.compile/</</<@http://localhost:8000/app2/lib/angular/angular.js:17823 Scope.prototype.$eval@http://localhost:8000/app2/lib/angular/angular.js:11906 Scope.prototype.$apply@http://localhost:8000/app2/lib/angular/angular.js:12006 ngEventDirectives[directiveName]</<.compile/</<@http://localhost:8000/app2/lib/angular/angular.js:17822 createEventHandler/eventHandler/<@http://localhost:8000/app2/lib/angular/angular.js:2610 forEach@http://localhost:8000/app2/lib/angular/angular.js:309 createEventHandler/eventHandler@http://localhost:8000/app2/lib/angular/angular.js:2609
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
217 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should use some other name for function $scope.answer

    $scope.answer1 = function(ans) {
        console.log('clicked' + ans);
    }

HTML:

<ul>
    <li ng-repeat="answer in question.answers">
        <button type="button" ng-click="answer1($index)">{{answer}} ({{$index}})</button>
    </li>
</ul>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...