2013-07-19 29 views
6

我正在玩Angular并编写一个Regex测试程序。 问题是我输入数据时导致空白被裁剪。请参阅示例jsfiddle here如何在使用输入标签时保留前导空白和尾随空白?

因此,当页面加载时,我有RegEx“^ \ d + $”。test(“123”)导致“不匹配”,但是如果在输入一个额外的前导或尾部空格候选框:

  1. 开头和结尾的空格从我的变量
  2. 结果中删除改变 “匹配”

这里是我的HTML:

<div id='ng:app' class='ng-app: myApp' ng-app='myApp'> 
    <div ng-controller="Controller">{{addTodo2()}} 
     <form novalidate class="simple-form">Pattern: 
      <input type="text" ng-model="pattern" />Candidate: 
      <input type="text" ng-model="candidate" /> 
      <br />.{{candidate}}. 
      <br>.{{candidate2}}.</form> 
    </div> 
</div> 

这里是相关联的JavaScript:

function Controller($scope) { 
    $scope.pattern = "^\\d+$"; 
    $scope.candidate = " 123 "; 
    $scope.candidate2 = " 123 "; 
    $scope.addTodo2 = function() { 
     var str = "Javascript is an interesting scripting language"; 
     var re = new RegExp($scope.pattern, "g"); 

     var result = re.test($scope.candidate); 
     if (result) { 
      return "Match22"; 
     } else { 
      return "No Match22"; 
     }; 
    }; 

    } 
var myapp = angular.module('myApp', []); 
+1

你见过或尝试纳克-trim =“false”在你的输入中? – shaunhusain

回答

11

更新了小提琴,加入NG-装饰= “假” 的输入标签

http://jsfiddle.net/T2zuV/12/

<div id='ng:app' class='ng-app: myApp' ng-app='myApp'> 
    <div ng-controller="Controller">{{addTodo2()}} 
     <form novalidate class="simple-form">Pattern: 
      <input type="text" ng-model="pattern" ng-trim="false"/>Candidate: 
      <input type="text" ng-model="candidate" ng-trim="false"/> 
      <br />.{{candidate}}. 
      <br>.{{candidate2}}.</form> 
    </div> 
</div> 
+0

谢谢。奇怪的是ng-trim不在1.1.5的文档中(可能为什么我没有看到它),但它在1.1.1的文档中。我快速浏览了1.1.5的源代码,它仍然存在,但它不在快照的源代码中。 – MrSteve

+0

@MrSteve对不起,不知道该怎么告诉你有关文档......我碰巧看到了这个指令,并且知道它处理了你遇到的情况,我想应该有一种方法来对文档作出贡献。 – shaunhusain

+0

好的关于文档的说明。虽然在最近的稳定版本1.0.7中找不到ng-trim:[link](http://docs.angularjs.org/api/ng.directive:input.text)。看起来ng-trim是在1.1.1中引入的 – MrSteve