angularjs - 只有最后一个单选按钮触发 $viewChangeListener

标签 angularjs validation

我有一组单选按钮和一个表单中的文本输入。文本输入应用了一些验证,这取决于所选的单选按钮。

HTML:

<form name="myForm" novalidate>
    <div class="radio">
        <input type="radio" ng-model="myValue" name="radioGroup" value="1">1</input>
        <input type="radio" ng-model="myValue" name="radioGroup" value="2">2</input>
        <input type="radio" ng-model="myValue" name="radioGroup" value="3">3</input>
    </div>
    <div class="otherFormComponent">
        <input type="text" ng-model="myTextValue" name="textValue" special-validator></input>
    </div>
</form>

JS:
app.directive('specialValidator', function(){
    return{
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl){
            ctrl.$validators.specialValidator = function(modelValue, viewValue){
                //Do some validation
            };

            //Force validation when a radio button changes
            scope.myForm.radioGroup.$viewChangeListeners.push(function(){
                scope.myForm.textValue.$validate();
            });
        }
    };
});

不幸的是,scope.myForm.textValue.$validate()代码仅在选择最后一个单选按钮 (3) 时触发。选择其他单选按钮不会触发添加到 scope.myForm.radioGroup.$viewChangeListeners 的方法.

如何设置单选按钮,以便更改任何单选按钮都会触发 viewChangeListeners ?

最佳答案

我向 angular 报告了这个错误:https://github.com/angular/angular.js/issues/15696

他们建议使用不同的 name不同输入的属性。

关于angularjs - 只有最后一个单选按钮触发 $viewChangeListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34703156/

相关文章:

javascript - 仅当在 angular.js 中选中所有复选框以获取动态加载 View 时,如何启用按钮

javascript - 使用 LocalStorage 和 AngularJS 重新思考数据结构

AngularJS:如何格式化 ISO8601 日期格式?

javascript - 如何使用 AngularJS Controller 中 &lt;script&gt; 标签中包含的库

php - 在使用 CodeIgniter 的表单验证类之前处理输入

validation - Grails 命令对象自定义验证消息代码

javascript - 在HTML音频标签中提供动态URL

使用 FOR 循环的 Javascript 表单验证

c# - asp.net 中的文本框验证 - 删除空格

asp.net - requestValidationMode 2.0 和 4.0 有什么区别