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($compile) {
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) {
}
};
})
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):
And its usage would be:
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: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 thengModel
pipelines, i.e. theconstructRangeString()
function. Using the parser/formatter, thectrl.theRange
variable gets the desired string format, while keeping the code more modularized (theconstructRangeString()
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/