Angular Data Binding - Input type=“number”

前端 未结 5 1963
眼角桃花
眼角桃花 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:55

    TypeScript version inspired ainos984, for the sake of posterity

         export class ngIntegerDirective implements ng.IDirective {
    
            static directiveKey: string = 'ngInteger';
    
            require: string = 'ngModel';
    
            link = (scope, ele, attr, ctrl: ng.INgModelController) => {
                ctrl.$parsers.unshift(function (viewValue) {
                    let result: number = parseInt(viewValue,10);
                    if (isNaN(result)) {
                        result = 0;
                    }
                    return result;
                });
            }
    
            public static Factory(): ng.IDirectiveFactory {
                const directive = () => new ngIntegerDirective();
                directive.$inject = []; //injecter les dépendances ici
                return directive;
            }
        }
    

提交回复
热议问题