2015-04-03 53 views
1

我想在一個寫入Razor的文本框中遵循指令[numericOnly]?MVC 5.0中的AngularJs指令

@Html.TextBoxFor(m => m.salary, new { data_ng_model = "salary" }) 

app.directive('numericOnly', function() { 

    return { 
     restrict: 'A', 
     require: '?ngModel', 
     link: function (scope, element, attrs, ngModel) { 
      if (!ngModel) return; 
      ngModel.$parsers.unshift(function (inputValue) { 
       var digits = inputValue.split('').filter(function (s) { return (!isNaN(s) && s != ' '); }).join(''); 

       if (digits.length > 6) { 
        digits = digits.substring(0, 5); 
       } 

       ngModel.$viewValue = digits; 
       ngModel.$render(); 
       return digits; 
      }); 
     } 
    }; 
}); 

請推薦任何人..謝謝。

回答

1

作爲指令應在小的情況下被寫爲與-分離,駱駝情況用小寫字母和-(連字符)等numericOnly成爲numeric-only

@Html.TextBoxFor(m => m.salary, new { data_ng_model = "salary", type = "text", numeric_only = ""}) 

結果

<input type="text" data-ng-model="salary" numeric-only="" /> 
+0

pankajparkar取代優秀!謝謝 – user3560308 2015-04-06 07:03:58