AngularJS ng-pattern regEx在使用templateCache时无法正常工作

时间:2015-02-18 23:57:20

标签: angularjs ng-pattern

我有以下文本字段,我使用ng-pattern进行验证并使用ng-message显示消息。验证适用于正则表达式(/ ^ \ d + $ /或/ ^ [0-9] + $ /),但由于某种原因,即使对于数字输入,正则表达式(/ ^ \ d + $ /)也返回false同时使用模板缓存。我无法弄清楚这两种方法之间的问题。请找到以下templateCaching输出。任何帮助表示赞赏。

  

input type =“text”ng-model =“appId”name =“appId”class =“new-app”ng-pattern =“/ ^ \ d + $ /”placeholder =“输入应用程序ID”必需< / p>

消息:

  

div ng-message =“pattern”class =“error-messages”

不使用templateCaching:

  

input type =“text”ng-model =“appId”name =“appId”class =“new-app”ng-pattern =“/ ^ \ d + $ /”placeholder =“输入应用程序ID”必需< / p>

使用templateCaching和单独的视图文件:

input type =“text”ng-model =“appId”name =“appId”class =“new-app”ng-pattern =“/ ^ [0-9] + $ /”placeholder =“输入申请表ID“必需

  

$ templateCache.put('/ views / layouts / test.html','&lt; input type =“text”ng-model =“appId”name =“appId”class =“new-app”ng-pattern =“/ ^ [0-9] + $ /”placeholder =“输入应用程序ID”必需&gt;   &lt; input type =“text”ng-model =“appId”name =“appId”class =“new-app”ng-pattern =“/ ^ \ d + $ /”placeholder =“输入应用程序ID”必需&gt;' );

1 个答案:

答案 0 :(得分:0)

我遇到了与templateCache类似的问题,这是因为模式中有特殊字符,如正斜杠,而我的验证失败了。转义它们解决了这个问题:所以如果你在html文件中有ng-pattern="/^\d+$/"而不是在templateCache语句中变成ng-pattern="/^\\d+$/"