AngularJS required radio buttons needs two click events to be valid

前端 未结 8 2039
庸人自扰
庸人自扰 2020-12-15 19:43

I have a very simple form where a radio button is required to be selected in order for a form to be valid. The radio buttons are generated by ngRepeat.

8条回答
  •  猫巷女王i
    2020-12-15 19:57

    Some times the $digest cycle dosen't $apply(fn) because you have two o more instances. For fix this you need $apply this trick manually, so put this in your directives:

    angular.('myApp',[])
    .directive('ngRadioExtend', ['$rootScope', function($rootScope){
            return {
                require: 'ngModel',
                restrict: 'A',
                link: function(scope, iElm, iAttrs, controller) {
                    iElm.bind('click', function(){
                        $rootScope.$$phase || $rootScope.$apply()
                    });
                }
            };
        }])
    

    and use it as:

    
    
    
    

    DONE it's the correct way!

提交回复
热议问题