2016-07-26 10 views
0

オンラインでカスタムディレクティブを使用していますが、実際にはうまく動作しますが、例えばディレクティブimのように10進数を制限するか、 int、私はカンマやドットを使うことができますが、唯一の問題は、サーバからデータをロードするときに、ドットが付いたデータベースで、最初にdbからロードされたときにカンマ点。私は上記の$ parsers関数を "replace( '。'、 '、')を使用して置き換えようとしましたが、常に変更していましたが、代わりにパッティングやドットまたはコンマを入れないようにしました。デシベル、それはカンマが付属していますカスタムディレクティブの数と種類の制限

指令:あなたのディレクティブで

angular.module('myApp') 
    .directive('onlyNumber', function() { 

     return { 
      require: 'ngModel', 
      link: function ($scope, elem, attrs, ngModel) { 
       var decRegexp, intRegexp; 

       intRegexp = /^(\d*)/; 
       decRegexp = "^(\\d*(\\.|,)?(\\d{1,DECIMALS})?)"; 
       decRegexp = new RegExp(decRegexp.replace('DECIMALS', ''+attrs.decimalUpto)); 

    // I tried this above but isnt updating the input/view 
var getter = $parse(attrs.ngModel); 
      var value = getter($scope); 
      if(value){ 
      ngModel.$setViewValue(value.replace('.', ',')); 
      ngModel.$render(); 
      } 


       ngModel.$parsers.push(function (val) { 


        var isDec, parsed, ref, regexp; 
        isDec = attrs.numType === 'decimal'; 

        regexp = isDec ? decRegexp : intRegexp; 
        parsed = val != null ? (ref = val.match(regexp)) != null ? ref[0] : void 0 : void 0; 
        ngModel.$setViewValue(parsed); 
        ngModel.$render(); 

        if(isDec){ 

         var result = parseFloat(parsed.replace(',', '.')); 
         if (attrs.minLimit > result) { 
          ngModel.$setValidity('smartFloatMin', false); 
          return undefined; 
         }else 
          ngModel.$setValidity('smartFloatMin', true); 



         if (attrs.maxLimit < result) { 
          ngModel.$setValidity('smartFloatMax', false); 
          return undefined; 
         }else 
          ngModel.$setValidity('smartFloatMax', true); 
        }else{ 
         var result = parseInt(parsed); 

         if (attrs.minLimit > result) { 
          ngModel.$setValidity('smartIntegerMin', false); 
          return undefined; 
         }else 
          ngModel.$setValidity('smartIntegerMin', true); 



         if (attrs.maxLimit < result) { 
          ngModel.$setValidity('smartIntegerMax', false); 
          return undefined; 
         }else 
          ngModel.$setValidity('smartIntegerMax', true); 
        } 


        return result; 
       }); 

      } 
     }; 
    }); 
+0

のようにそれを使用します) $ parsers.push()の代わりに。 viewValeの$ parsers.pushはmodelValueではありません! – praHoc

+0

nop。 doesnt仕事。 –

+0

ビューコードを表示できますか? – praHoc

答えて

0

$ formaters.pushを(試してみてください。この

ngModelCtrl.$formatters.push(function(modelValue) { 
    return modelValue.replace(/\./g,',') ; 
    }) 

here is a working plunker