http://jsfiddle.net/crimsonalucard/238u6/
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;
}
HTML:
<div ng-app="myModule">
<div ng-controller="myController">
<input add-colons type="text" ng-model="time"/>
<p>How do I get the text in the input field above to dynamically change to be equivalent to the string below as I am typing?</p>
<p>$scope.time = {{time}}</p>
</div>
</div>
上記の JSFiddle を参照してください。入力フィールドのテキストを、$scope.time で見られるフォーマットで動的に更新したいと考えています。これはどのように行われますか?
したがって、基本的に入力フィールドに (0000000) と入力すると、入力フィールド自体 ($scope.time だけでなく) を動的に変更して、$scope.time を (0:00:00:00) にフォーマットするものと一致するようにします。 .
(編集:)Angular自体以外のライブラリを使用しないようにソリューションを希望します。angular-UI mask ディレクティブが可能な解決策であることは認識していますが、これは私が探しているものではありません。