AngularJs - bind one ng-model to directive with two inputs

后端 未结 1 1471
情话喂你
情话喂你 2020-12-17 05:44

How to create a range directive which binds to one ng-model and outputs two input fields using a filter (already made). E

相关标签:
1条回答
  • 2020-12-17 06:24

    The correct way (IMO) is to create a custom control as described here.

    As an exercise, I implemented it in this fiddle: http://jsfiddle.net/6cn7y/

    The code of the directive is (you may need to adapt some details):

    app.directive("range", function() {
        var ID=0;
    
        function constructRangeString(from, to) {
            var result;
            if( !from && !to ) {
                result = null;
            }
            else if( from ) {
                result = from + "/" + (to || "");
            }
            else if( to ) {
                result = "/" + to;
            }
            return result;
        }
    
        return {
            require: "ngModel",
            restrict: "E",
            replace: true,
            scope: {
                name: "@"
            },
            template:
                '<div ng-form="{{ subFormName }}">' +
                    '<input type="text" ng-model="from" class="range-from" />' +
                    '<input type="text" ng-model="to" class="range-to" />' +
                '</div>',
            link: function(scope,elem,attrs,ngModel) {
                var re = /([0-9]+)\/([0-9]+)/;
    
                if( scope.name ) {
                    scope.subFormName = scope.name;
                }
                else {
                    scope.subFormName = "_range" + ID;
                    ID++;
                }
    
                ngModel.$render = function() {
                    var result, from, to;
                    result = re.exec(ngModel.$viewValue);
                    if( result ) {
                        from = parseInt(result[1]);
                        to = parseInt(result[2]);
                    }
                    scope.from = from;
                    scope.to = to;
                };
    
                scope.$watch("from", function(newval) {
                    var result = constructRangeString(newval, scope.to);
                    ngModel.$setViewValue(result);
                });
                scope.$watch("to", function(newval) {
                    var result = constructRangeString(scope.from, newval);
                    ngModel.$setViewValue(result);
                });
            }
        };
    });
    

    And its usage would be:

    <range ng-model="ctrl.theRange" name="myRange" required="true"></range>
    

    I doubt the filters will get you anywhere with this, as they do not do 2-way binding.


    EDIT: Even though this solves the problem, I would suggest a slightly different approach. I would define the model of the range directive as an object:

    {
        from: ...,
        to:   ...
    }
    

    This means that the output in the ctrl.theRange variable in the example would be an object like that of the above. If you really want the string format "from/to", add a parser/formatter in the ngModel pipelines, i.e. the constructRangeString() function. Using the parser/formatter, the ctrl.theRange variable gets the desired string format, while keeping the code more modularized (the constructRangeString() function is external to the directive) and more parameterized (the model is in a format that can easilly be processed and transformed).

    And a proof of concept: http://jsfiddle.net/W99rX/

    0 讨论(0)
提交回复
热议问题