Setting ngTrueValue and ngFalseValue to numbers

前端 未结 4 569
轻奢々
轻奢々 2020-12-28 14:23

Update: question is obsolete for latest Angular version, see tsh\'s comment on this post


I have bound a checkbox to a value:

&         


        
4条回答
  •  天涯浪人
    2020-12-28 14:50

    I have created directive for that, seems to work fine:

    angular.module('app').directive('cdTrueValue', [function() {
      return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attrs, ngModel) {
          ngModel.$parsers.push(function(v){
            return v ? scope.$eval(attrs.cdTrueValue) : scope.$eval(attrs.cdFalseValue);
          });
    
          ngModel.$formatters.push(function(value) {
              return value === scope.$eval(attrs.cdTrueValue);
          });
        }
      };
    }]);
    

    Usage:

    
    

    DEMO

提交回复
热议问题