使用指令的替代解决方案。在Firefox(v 33.0)中,已接受的答案对我不起作用。
想法是在更改时,在具有特定类名称的所有无线电中将required属性设置为false。
- 添加jQuery是因为我在使用jqlite remove属性功能时遇到麻烦。
- 我尽可能从原始的竖笛复制。
http://plnkr.co/edit/nbzjQqCAvwNmkbLW5bxN?p=preview
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-radio-input-directive-production</title>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.14/angular.min.js"></script>
</head>
<body ng-app="radioExample">
<script>
angular.module('radioExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.myObject= {};
$scope.specialValue = {
"id": "12345",
"value": "green"
};
$scope.submitForm = function() {
alert('valid');
}
}])
.directive('colorChoice', function() {
return {
restrict: 'E',
template: '<div ng-repeat="c in colors"><input class="colorClass" type="radio" value={{c}} ng-model="myObject.color" required />{{c}}</div>',
link: function(scope, element, attrs) {
scope.colors = ['Red', 'Green', 'Blue'];
element.on('change', function(){
$(".colorClass").attr("required", false);
});
}
}
});
</script>
<form name="myForm" ng-submit="submitForm()" ng-controller="ExampleController">
<color-choice></color-choice>
<tt>color = {{myObject.color | json}}</tt><br/>
<button type="submit">Submit</button>
</form>
</body>
</html>
ng-required
而不是仅仅required
这样做。谢谢。