简体   繁体   English

如何在AngularJS中过滤输入值

[英]how to filter input value in AngularJS

I'm trying to implement filtering on my input element. 我正在尝试对输入元素实施过滤。 I want to make filtering for input with type="text" field. 我想使用type =“ text”字段对输入进行过滤。 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. 我创建了jsfiddle,我有一条指令可以动态生成html模板,它包含输入字段。

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. 我有很多业务逻辑,并且我不想将代码保留在控制器中,因为它将是可重用的指令。

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) 这是文档中的示例- 实现自定义表单控件(使用ngModel)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM