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/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…