JSFiddle - React, Tailwind, and code Playground
by crimsonalucard
HTML
<div ng-app="myModule">
<div ng-controller="myController">
<input add-colons type="text" ng-model="time"/>
<p>$scope.time = {{time}}</p>
</div>
</div>
JavaScript
var myModule = angular.module('myModule', []);
myModule.controller('myController', function($scope){
});
myModule.directive('addColons', function(){
return{
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ngModel){
ngModel.$parsers.push(addColons);
ngModel.$formatters.push(addColons);
}
};
});
function insert(text, insertion, index)
{
return text.slice(0,index) + insertion + text.slice(index);
}
function addColons(text)
{
if(text.length !== undefined)
{
console.log("length is: " + text.length);
for(var i = text.length-2; i>0; i-=2)
{
if(text.charAt(i) !== ':')
{
text = insert(text, ":", i);
}
}
}
return text;
}