【问题标题】:How to autocapitalize an input field in angular js? [duplicate]如何在angular js中自动大写输入字段? [复制]
【发布时间】:2015-07-17 18:58:51
【问题描述】:

我有一个输入框。

我需要自动大写这个框。

是否有任何可用的属性或者我需要在控制器上编码

我还需要将此效果应用于角度模型

【问题讨论】:

标签: javascript jquery html angularjs


【解决方案1】:

您可以在使用 ng-change 更改该字段时使用角度过滤器 uppercase,如果您想使其成为负载资本,那么您也应该在 ng-init 中使用同样的东西

标记

<input type="text" ng-model="sample" ng-change="sample=(sample|uppercase)"/>

【讨论】:

    【解决方案2】:

    是的,我们对此有一个指令 :) 原始代码(用 TypeScript 编写)在 GitHub https://github.com/ST-Software/STAngular/blob/master/src/directives/SgUpperCase.ts

    //修复光标在末尾跳转的bug

    someModule.directive("sgUpperCase", [function () {
        function getCaretPosition(inputField) {
            // Initialize
            var position = 0;
            // IE Support
            if (document.selection) {
                inputField.focus();
                // To get cursor position, get empty selection range
                var emptySelection = document.selection.createRange();
                // Move selection start to 0 position
                emptySelection.moveStart('character', -inputField.value.length);
                // The caret position is selection length
                position = emptySelection.text.length;
            }
            else if (inputField.selectionStart || inputField.selectionStart == 0) {
                position = inputField.selectionStart;
            }
            return position;
        }
        function setCaretPosition(inputElement, position) {
            if (inputElement.createTextRange) {
                var range = inputElement.createTextRange();
                range.move('character', position);
                range.select();
            }
            else {
                if (inputElement.selectionStart) {
                    inputElement.focus();
                    inputElement.setSelectionRange(position, position);
                }
                else {
                    inputElement.focus();
                }
            }
        }
        return {
            require: "^ngModel",
            restrict: "A",
            link: function (scope, elm, attrs, ctrl) {
                var toUpperCase = function (inputValue) {
                    if (!inputValue)
                        return inputValue;
                    var modified = inputValue.toUpperCase();
                    if (modified !== inputValue) {
                        var position = getCaretPosition(elm[0]);
                        ctrl.$setViewValue(modified);
                        ctrl.$render();
                        setCaretPosition(elm[0], position);
                    }
                    return modified;
                };
                ctrl.$parsers.push(toUpperCase);
                toUpperCase(scope[attrs.ngModel]); //Transform initial value
            }
        };
    }]);
    

    【讨论】:

    • 如果您在字段中间键入,这也会使光标跳到末尾。
    • @MattiVirkkunen 感谢您指出这一点,您是对的。我会解决的
    • @MattiVirkkunen 已修复
    【解决方案3】:

    看看它的工作原理..它还更新了模型...

    <div ng-app="HelloApp">
        <input type="text" ng-model="name" uppercased/> {{name}}        
    </div>
    
    angular.module('components', [])
       .directive('uppercased', function() {
        return {
            require: 'ngModel',        
            link: function(scope, element, attrs, modelCtrl) {
                modelCtrl.$parsers.push(function(input) {
                    return input ? input.toUpperCase() : "";
                });
                element.css("text-transform","uppercase");
            }
        };
    });
    
    angular.module('HelloApp', ['components'])
    

    工作模型here

    【讨论】:

      【解决方案4】:

      将此添加到您的controller$watch 将侦听对 bic 所做的任何更改并更新值。

      $scope.$watch('bic', function () {
          if ($scope.bic) {
              $scope.bic = $scope.bic.toUpperCase();
          }
      });
      

      【讨论】:

      • 这会使光标在每次键入内容时跳到末尾,如果您要进行更正,这会很烦人。
      【解决方案5】:

      将类添加到输入标签中。 .大写{ 文本转换:大写“ }

      i.e <input type="text" ng-model="bic" class="capitalize" id="txtbic" maxlength="11" class="span6" value="" name="">
      

      【讨论】:

        【解决方案6】:

        这个你不需要 Jquery/Javascript。试试这个:

        <input type="text" ng-model="abc"   id="myid" class="span6" value="" name="">
        

        在 CSS 中

        input {
            text-transform: uppercase;
        }
        

        JSFIDDLE DEMO

        【讨论】:

        • 我有点喜欢这样,它非常不显眼,但如果你想让你的字符串实际上是大写的,你还需要在 JS 中应用转换。 (但是 jQuery 和什么有什么关系呢?)
        • @MattiVirkkunen:- 同意,我说的是 jquery,因为问题是用它标记的 ;)
        • 哦,好吧。我希望人们不再将所有东西都标记为 jQuery,因为使用 Angular 你很少需要它......
        【解决方案7】:

        您实际上可以使用 CSS 来做到这一点。

        input{
            text-transform: uppercase;
        }
        &lt;input type="text" value="This text is auto capitalized" /&gt;

        【讨论】:

        • 在我的代码中它只显示第一个单词
        • @Ervikas,你用的是什么浏览器?
        • CSS 转换确实转换了字段的值,因此普通表单提交将包含大写的值。但是,David Votrubec 在另一条评论中提到它不会改变模型在角度方面的基础价值
        • 虽然这可能有效,但通过 CSS 转换输入值不会反映在角度 model 中。为了使大写是永久性的,它必须由 Angular 应用。
        • 是的,你是对的@EdB。它不会影响需要将大写数据也发送到控制器的模型
        猜你喜欢
        • 2014-10-02
        • 1970-01-01
        • 2018-10-30
        • 2013-02-20
        • 1970-01-01
        • 1970-01-01
        • 2018-06-07
        • 2023-03-10
        • 2017-08-02
        相关资源
        最近更新 更多