Javascript AngularJs Radiobox组验证
Angularjs是否有一种简单的方法来验证已在额外指令中实例化的放射组 验证要求:除非单击其中一个RadioBox,否则用户无法提交 下面是我如何构建它的: HTML文档:Javascript AngularJs Radiobox组验证,javascript,angularjs,validation,angularjs-directive,Javascript,Angularjs,Validation,Angularjs Directive,Angularjs是否有一种简单的方法来验证已在额外指令中实例化的放射组 验证要求:除非单击其中一个RadioBox,否则用户无法提交 下面是我如何构建它的: HTML文档: <ng-form name="salutationgroup"> <radiotrigger model="mrms" name="gender" value='1' label="mr" ></radiotrigger> <radiotrigger mod
<ng-form name="salutationgroup">
<radiotrigger model="mrms" name="gender" value='1' label="mr" ></radiotrigger>
<radiotrigger model="mrms" name="gender" value='2' label="ms" ></radiotrigger>
</ng-form>
JS文档:
Directives.directive('radiotrigger', function() {
return {
restrict: 'E',
scope: {},
compile: function(element, attrs) {
var templateText = '<li><label><input type="radio" ng-model="' +
attrs.model + '" value="' +
attrs.value + '">' +
attrs.label + '</label></li>';
element.replaceWith(templateText);
}
};
});
指令('radiotrigger',function()){
返回{
限制:'E',
作用域:{},
编译:函数(元素、属性){
var templateText='
指令(注意“ng change=”enabled=true“):
指令('radiotrigger',function()){
返回{
限制:'E',
作用域:{},
编译:函数(元素、属性){
var templateText='我猜您也错过了传递
name
属性,因此只有一个值是可选的?另一方面,设置required
属性-ngForm将获得ng invalid
和ng invalid required
css类,这些类让您知道您的ngForm是有效的。@naeramarth7,它似乎总是会将未选中的单选按钮自身设置为无效。好吧,让我们简单地说。<ng-form name="salutationgroup">
<radiotrigger model="mrms" name="gender" value='1' label="mr"></radiotrigger>
<radiotrigger model="mrms" name="gender" value='2' label="ms"></radiotrigger>
<input type="submit" ng-disabled="!enabled"></input>
</ng-form>
Directives.directive('radiotrigger', function() {
return {
restrict: 'E',
scope: {},
compile: function(element, attrs) {
var templateText = '<li><label><input type="radio" ng-model="' +
attrs.model + '" value="' +
attrs.value + '" ng-change="enabled=true">' +
attrs.label + '</label></li>';
element.replaceWith(templateText);
}
};