Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

javascript - Angularjs - repeat and click

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

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

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...