我被要求在网页上显示是/否单选按钮。在初始状态下,这些选项都未被选中。需要验证以确保用户选择其中的一个。我能做到这一点使用的浏览器自身的表单验证,就像这样:使用Angular验证单选按钮的选择
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-radio-input-directive-production</title>
<script src="angular.min.js"></script>
</head>
<body ng-app="radioExample">
<script>
angular.module('radioExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.submitForm = function() {
alert('valid');
}
}]);
</script>
<form name="myForm" ng-submit="submitForm()" ng-controller="ExampleController" novalidate>
<input name="question" type="radio" ng-model="question" value="yes" required>Yes<br/>
<input name="question" type="radio" ng-model="question" value="no">No<br/>
<pre>myForm.question.$error = {{ myForm.question.$error | json }}</pre>
<div ng-messages="myForm.question.$error" role="alert">
<div ng-message="required">You did not enter a field</div>
</div>
<button type="submit">Submit</button>
</form>
</body>
</html>
我希望能够利用的角度,以显示在NG-消息的验证消息,但我在努力使它工作:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-radio-input-directive-production</title>
<script src="angular.min.js"></script>
</head>
<body ng-app="radioExample">
<script>
angular.module('radioExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.submitForm = function() {
alert('valid');
}
}]);
</script>
<form name="myForm" ng-submit="submitForm()" ng-controller="ExampleController" novalidate>
<input name="question" type="radio" ng-model="question" value="yes" required>Yes<br/>
<input name="question" type="radio" ng-model="question" value="no">No<br/>
<pre>myForm.question.$error = {{ myForm.question.$error | json }}</pre>
<div ng-messages="myForm.question.$error" role="alert">
<div ng-message="required">You did not enter a field</div>
</div>
<button type="submit">Submit</button>
</form>
</body>
</html>
我做错了什么?