【问题标题】:get value radiobutton in controller and check (angular)获取控制器中的值单选按钮并检查(角度)
【发布时间】:2014-01-12 19:52:20
【问题描述】:

我正在创建一个小测验,我从一个 json 文件中获取我的问题和答案。回答完一个问题后,您需要单击“下一个问题”按钮。通过这样做,我需要检查答案是否正确(还要记住,因为我想在最后显示总分)。

我该怎么做?我真的不知道,我刚开始使用角度......

HTML

<div ng-repeat="question in questions | filter:ids">
{{question.question}}
<div ng-repeat="answer in question.answers"><input type="radio" name="rAnswer" value="{{answer.answer}}">{{answer.answer}}
</div>
<input type="button" ng-click="next()" value="{{buttonText}}">

控制器

lycheeControllers.controller('quizCtrl', ['$scope', '$http', function ($scope, $http) {
$http.get('json/questions.json').success(function (data) {
    //all questions
    $scope.questions = data;

    //filter for getting answers / question
    $scope.ids = function (question) {
        return question.id == number;
    }

    $scope.buttonText = "Next question";

    $scope.next = function () {
        if (!(number == (data.length))) {
            if(number+1==(data.length)){
                $scope.buttonText = "Get results";
            }
            number++;
        }
    }
});

}]);

【问题讨论】:

    标签: angularjs controller radio-button


    【解决方案1】:

    我只需将 ng-model="answer.selected" 添加到 ng-repeated input。现在您可以循环查看 answer.selected 的正确位置,这就是您的答案。

    如果您不想弄乱您的模型,另一种方法是在控制器中执行$scope.response = []; 之类的操作,然后在模板中设置ng-model="response[$index]"。然后 $scope.response 将保存所有值,您可以循环查看数据在哪里。

    附带说明:您可能还会发现设置 $scope.question 比设置过滤技术更容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 2020-06-13
      相关资源
      最近更新 更多