JSFiddle - React, Tailwind, and code Playground
HTML
<div ng-app="app" ng-controller="Ctrl as ctrl">
<form name="theform">
<range ng-model="ctrl.theRange" name="myRange" required="true" string-range-formatter=""></range>
</form>
OUTPUT: {{ ctrl.theRange }}
<button ng-click="ctrl.theRange = '10/100'">SET</button>
</div>
JavaScript
var app = angular.module("app",[]);
app.controller("Ctrl", function($scope) {
this.theRange = "1/7";
});
app.service("rangeString", function() {
var RE = /([0-9]+)\/([0-9]+)/;
function construct(from, to) {
var result;
if( !from && !to ) {
result = null;
}
else if( from ) {
result = from + "/" + (to || "");
}
else if( to ) {
result = "/" + to;
}
return result;
}
function deconstruct(str) {
var result, from, to;
result = RE.exec(str);
if( result ) {
from = parseInt(result[1]);
to = parseInt(result[2]);
}
return { from: from, to: to };
}
return {
construct: construct,
deconstruct: deconstruct
};
});
app.directive("stringRangeFormatter", function(rangeString) {
return {
restrict: "A",
require: "ngModel",
link: function(scope,elem,attrs,ngModel) {
ngModel.$parsers.push(function(val) {
var result;
if( val ) {
result = rangeString.construct(val.from, val.to);
}
return result;
});
ngModel.$formatters.push(rangeString.deconstruct);
}
};
});
app.directive("range", function() {
var ID=0;
return {
require: "ngModel",
restrict: "E",
replace: true,
scope: {
name: "@"
},
template:
'<div ng-form="{{ subFormName }}">' +
'<input type="text" ng-model="model.from" class="range-from" />' +
'<input type="text" ng-model="model.to" class="range-to" />' +
'</div>',
link: function(scope,elem,attrs,ngModel) {
if( scope.name ) {
scope.subFormName = scope.name;
}
else {
scope.subFormName = "_range" + ID;
...