Two-way binding with range and number input in AngularJS

前端 未结 3 473
梦毁少年i
梦毁少年i 2021-02-07 06:39

I\'m just starting to play around with AngularJS and trying to understand the binding technique. For starters, I tried to make a simple conversion calculator (dozens to pieces,

3条回答
  •  礼貌的吻别
    2021-02-07 07:19

    I think this solution is more generic.

    myApp.directive('input', function() {
    return {
        restrict: 'E',
        require: '?ngModel',
        link: function(scope, element, attrs, ngModel) {
            if ('type' in attrs && attrs.type.toLowerCase() === 'range') {
                ngModel.$parsers.push(parseFloat);
            }
        }
    };
    

    });

    full explanation

提交回复
热议问题