简体   繁体   English

验证是否在提交页面上选中了单选按钮:AngularJs

[英]Validate if radio buttons are checked in a page on submit: AngularJs

I am new to AngularJS. 我是AngularJS的新手。 I have poulated the dom with some questions using factory. 我已经使用工厂向dom提出了一些问题。 I have to validate if the radio button are checked. 我必须验证是否选中了单选按钮。 Please give suggesstions to improve this code 请提出建议以改进此代码

Index.html Index.html

<body ng-app="quiz">
    <div id="container" ng-controller="QuizController"> 
        <button ng-click="submitfun()">submit</button>
    </div>  
</body>

app.js app.js

angular.module('quiz', ['quiz.service','quiz.directive']);

var QuizController = function($scope, questionFactory) {
    $scope.questions_and_answers = questionFactory;
    $scope.checkList=[];    
    $scope.submitfun= function(message){
        alert('hi');         
    }   
};

factory.js factory.js

angular.module('quiz.service', [])
    .factory('questionFactory', function() {
        return [
            { question: "Which of these is England's official slogan for the World Cup?", answerChoices: [ "Impossible is not an English word", "One nation, one team, one dream!", "Real men wear red and white","The dream of one team, the heartbeat of millions!!" ] },
            { question:"Who was Roy Keane talking this week about when he said: \"Not everybody wants to wander around the pitch waving and crying?\" ", answerChoices:["David Moyes","John Terry","Luis Suárez","Rio Ferdinand"]}
        ];
});

You can use the combination of ng-disabled and ng-show directives for validation and displaying error message. 您可以结合使用ng-disabled和ng-show指令进行验证和显示错误消息。

See this plnkr 看到这个plnkr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM