2

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.

Oleksii
  • 328
  • 3
  • 17

1 Answers1

0

I think that implementing this part of functionality as a filter is not the best idea.

It would be much more dynamic if you will implement it as directive on your input element like:

<input type="text" ng-model="myModel" ng-value="myModel" max-length="20" />

In this case it would be more flexible. You will be able to pass an argument into directive (for example length of acceptable value).

Also it is not really readable for another developers to make your input as a template of your directive because of you are using model as attribute of input field and not binding it from directive.

Is there any reason why you use directive to render simple input? If not, then just live it as input in your view and add directive instead of filter to work with data checks limitations.


Another approach is to implement custom form controls. That will allows you to control incoming and out-coming data.

Here is a example from documentation - Implementing custom form controls (using ngModel)

Dmitry Zaets
  • 3,289
  • 1
  • 20
  • 33
  • Thanks for advice. I will think about it. – Oleksii Apr 14 '15 at 16:57
  • I would definitely go for ngModelController because you're operating on formfields and won't duplicate your controller code everywhere your like to place your specific input field to your app. I had a similar problem with live i18n (without page refresh) number values of input fields, where i had to switch the number's group and decimal separators when switched from german to englisch locale. As U10 mentioned check out the ngModel. http://stackoverflow.com/a/15346236/1554767 That link opened my mind and brought me into the hell of the ngModel. – xam.esurk Apr 14 '15 at 19:38