I'm trying to implement filtering on my input element. I want to make filtering for input with type="text" field. For instance, if the model contain more than available characters than I want to change my input value. I've created jsfiddle I have directive that generate html template dynamically and it contains input field.
var app = angular.module('app', [])
.controller('ctrlr', function($scope){
$scope.myModel = "test";
$scope.availableCharacters = 5;
$scope.$watch('myModel', function(newValue, oldValue){
if(!newValue){
return;
}
if(newValue.length > 5){
$scope.cutString();
}
});
$scope.cutString = function(){
var length = $scope.myModel.length;
var string = $scope.myModel.slice(0, $scope.availableCharacters);
var countStars = length - string.length;
$scope.myModel = $scope.createStars(string, countStars);
}
$scope.createStars = function(string, countStars){
for(var i = 1; i <= countStars; i++){
string = string+'*';
}
return string;
}
})
.directive('awesome' , function(){
return {
restrict:'E',
template:'<input type="text" ng-model="myModel" ng-value="myModel | filter:my" />'
}
})
Could it possibly to move my code into the filter function? I have a lot of business logic and I don't want to keep my code in the controller, because it will be reusable directive.