如何使用 Angular JS 在输入字段中输入时将文本转换为大写

dan*_*ela 1 angularjs-directive

我知道这对于 jquery 是可能的,但我不知道如何使用 Angular js 做到这一点,请问有什么建议吗?

       function mayuscula(campo){
            $(campo).keyup(function() {
                           $(this).val($(this).val().toUpperCase());
            });
       }
Run Code Online (Sandbox Code Playgroud)

小智 5

您还可以为此创建一个指令!

检查代码:

directive('uppercase', function() {
return {
    restrict: "A"
    require: "?ngModel",
    link: function(scope, element, attrs, ngModel) {

        //This part of the code manipulates the model
        ngModel.$parsers.push(function(input) {
            return input ? input.toUpperCase() : "";
        });

        //This part of the code manipulates the viewvalue of the element
        element.css("text-transform","uppercase");
    }
};
})
Run Code Online (Sandbox Code Playgroud)

对于它的用法,这里有一个例子:

<input type="text" ng-model="myModel" uppercase />
Run Code Online (Sandbox Code Playgroud)