admin管理员组

文章数量:1303552

How to create a range directive which binds to one ng-model and outputs two input fields using a filter (already made). Essentially I've got one direction from model to input working, but the other side from input to model not. How to achieve this?

I've got this Html:

<div tu-range ng-model="arbitraymodel" />

And a model:

var arbitrarymodel = "10/22";

Side note; I created a filter to split those two values:
{{ feature.Value | split:\'/\':0}}

And this directive:

.directive('tuRange', function($pile) {
    return { 
        restrict: 'A',
        require: 'ngModel',
        scope: {
            feature: '=',
            tudisabled: '=',
            model: '=ngModel' // edited
        },
        template: '<input type="text" '+
               'ng-value="{{ model | split:\'/\':0}}" />'+ // edited to 'model'
             '-<input type="text" '+
               'ng-value="{{ model | split:\'/\':1}}" />', // edited to 'model'
        link: function(scope, element, attributes, ngModel) {

        }
    };
})

How to create a range directive which binds to one ng-model and outputs two input fields using a filter (already made). Essentially I've got one direction from model to input working, but the other side from input to model not. How to achieve this?

I've got this Html:

<div tu-range ng-model="arbitraymodel" />

And a model:

var arbitrarymodel = "10/22";

Side note; I created a filter to split those two values:
{{ feature.Value | split:\'/\':0}}

And this directive:

.directive('tuRange', function($pile) {
    return { 
        restrict: 'A',
        require: 'ngModel',
        scope: {
            feature: '=',
            tudisabled: '=',
            model: '=ngModel' // edited
        },
        template: '<input type="text" '+
               'ng-value="{{ model | split:\'/\':0}}" />'+ // edited to 'model'
             '-<input type="text" '+
               'ng-value="{{ model | split:\'/\':1}}" />', // edited to 'model'
        link: function(scope, element, attributes, ngModel) {

        }
    };
})
Share Improve this question edited Aug 5, 2014 at 14:57 Highmastdon asked Aug 5, 2014 at 14:47 HighmastdonHighmastdon 7,5307 gold badges46 silver badges71 bronze badges 3
  • 2 Do you really need to do it with the value of 10/22? Why don't use an Object for that, with arbitrary.begin and arbitrary.end keys? You can bine both back to one value if you want with a change listener. – ConcurrentHashMap Commented Aug 5, 2014 at 14:50
  • hm, not so sure about this, but how does he reverse the filtering for the two way binding? – Raphael Müller Commented Aug 5, 2014 at 14:51
  • I made a small change to the directive scope adding model. @ConcurrentHashMap Yes it's not the most optimal data I get. But eventually it has to be send back that way, so to make sure everything happens in one place, I'd like to do it in the directive. – Highmastdon Commented Aug 5, 2014 at 14:56
Add a ment  | 

1 Answer 1

Reset to default 8

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

As an exercise, I implemented it in this fiddle: http://jsfiddle/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/W99rX/

本文标签: javascriptAngularJsbind one ngmodel to directive with two inputsStack Overflow