【问题标题】:How prevent angular version earlier than 1.3.0 auto trim for fields?如何防止 1.3.0 之前的角度版本自动修剪字段?
【发布时间】:2015-05-31 12:34:41
【问题描述】:

有什么方法可以防止 1.3.0 之前的 Angular 版本自动修剪整个应用程序中的字段?我知道我可以使用 ngTrim 指令阻止指定字段使用它,但是将此指令添加到应用程序中的所有文本字段看起来并不好,有没有办法对角度模块中的所有字段执行此操作?这是代码,如果您在输入的开头添加空格,它们将不会出现在标签中:

<div ng-app>
  <div ng-controller="TodoCtrl">
      {{field}}
    <input type="text" ng-model="field">
  </div>
</div>

这里也是我的第一个问题How prevent angular auto trim for fields? 的链接,那里的答案适用于 angular 1.3.0 或更高版本。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    在 Angular 1.3.x 之前的版本中,您可以尝试装饰编译功能:

    app.config(function($provide) {
        $provide.decorator('inputDirective', function($delegate) {
    
            var directive = $delegate[0];
    
            directive.compile = function(orig) {
                return function(element, attrs, transclude) {
                    attrs.$set('ngTrim', 'false');
                    return orig.apply(null, arguments);
                };
            }(directive.compile);
    
            return $delegate;
        });
    });
    

    不过,这种方法也应该适用于 1.3.x。

    演示: http://plnkr.co/edit/gvIa7omiFWwoWW9tyl4S?p=preview

    【讨论】:

      猜你喜欢
      • 2015-08-12
      • 2011-02-04
      • 2010-10-11
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 2019-04-19
      相关资源
      最近更新 更多