Angular Data Binding - Input type=“number”

前端 未结 5 2018
眼角桃花
眼角桃花 2021-01-05 05:36

I\'m having problems binding a number value using AngularJS.

I\'ve put a simplified example on JSFiddle: http://jsfiddle.net/treerock/ZvdXp/

5条回答
  •  被撕碎了的回忆
    2021-01-05 05:59

    You can also fix this with a directive. I created a directive to force input bound to numeric fields to be numeric.

    Html:

    myApp.directive('numericbinding', function () {
            return {
                restrict: 'A',
                require: 'ngModel',
                scope: {
                    model: '=ngModel',
                },                
               link: function (scope, element, attrs, ngModelCtrl) {
                   if (scope.model && typeof scope.model == 'string') {
                       scope.model = parseInt(scope.model);
                   }                  
                }
            };
    });
    

    You can add it to your numeric field like this:

        
    

    full example: http://jsfiddle.net/tdjager/cMYQ3/1/

提交回复
热议问题