ng-model抛出角度为1.3的输入类型编号的错误

ak8*_*k85 12 javascript html5 angularjs angular-ngmodel

我有一个输入字段,我希望他用户输入一个数字,所以我创建了一个type ="number"的输入字段.

当我在1.2中使用它时,我没有错误

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular.min.js"></script>
    <script>
          var app = angular.module('app', []);
            app.controller('MainCtrl', ['$scope', function ($scope) {
                $scope.person = [
                {"name": "Alex","pts": "10"}
            ];
            }]);
    </script>
        <div ng-app="app">
                <div ng-controller="MainCtrl">
                  {{person | json }}<br>
                  name: <span ng-bind="person[0].name"></span></br>
                  <!-- pts: <input ng-model="person[0].pts"> -->
                  pts: <input type="number" ng-model="person[0].pts"><br?
            </div>
        </div>
Run Code Online (Sandbox Code Playgroud)

http://codepen.io/anon/pen/dPKgVL

但是当我在1.3中使用它时,我得到错误:[ngModel:numfmt]但是当我更新数字时,它似乎仍然绑定到范围.

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script>
          var app = angular.module('app', []);
            app.controller('MainCtrl', ['$scope', function ($scope) {
                $scope.person = [
                {"name": "Alex","pts": "10"}
            ];
            }]);
    </script>
        <div ng-app="app">
                <div ng-controller="MainCtrl">
                {{person | json }}<br>
                  name: <span ng-bind="person[0].name">
                  name: <span ng-bind="person[0].name"></span></br>
                  <!-- pts: <input ng-model="person[0].pts"> -->
                  pts: <input type="number" ng-model="person[0].pts">
            </div>
        </div>
Run Code Online (Sandbox Code Playgroud)

http://codepen.io/anon/pen/YPvJro

我在这里做错了什么或者这没什么好担心的?当我尝试调试其他问题时,我宁愿不在控制台中出现错误

Kyl*_*leM 19

添加此项以解决问题:

myApp.directive('input', [function() {
    return {
        restrict: 'E',
        require: '?ngModel',
        link: function(scope, element, attrs, ngModel) {
            if (
                   'undefined' !== typeof attrs.type
                && 'number' === attrs.type
                && ngModel
            ) {
                ngModel.$formatters.push(function(modelValue) {
                    return Number(modelValue);
                });

                ngModel.$parsers.push(function(viewValue) {
                    return Number(viewValue);
                });
            }
        }
    }
}]);
Run Code Online (Sandbox Code Playgroud)

  • 如果你没有完全控制你的JSON模型,这很方便(例如,我也必须支持该属性中的字符串,因此将其更改为整数类型对我来说不起作用). (2认同)

net*_*eet 11

pts属性定义为number而不是string:

var app = angular.module('app', []);
app.controller('MainCtrl', ['$scope', function ($scope) {
    $scope.person = [
        {"name": "Alex","pts": 10}
    ];
}]);
Run Code Online (Sandbox Code Playgroud)


chr*_*cpn 8

该指令将为任何类型为"number"的输入解析字符串值.那你就不会有错误:

module.directive('input', function(){
    return {
        require: 'ngModel',
        link: function(scope, elem, attrs, ngModel){
            if(attrs.type == 'number'){
                ngModel.$formatters.push(function(value){
                    return parseFloat(value);
                });
            }
        }
    };
});
Run Code Online (Sandbox Code Playgroud)