是有可能使用纳克图案具有可变 [英] Is it possible to use ng-pattern with a variable
问题描述
我是可怕的定期EX pressions但我想知道是否有可能采用NG-模式具有可变
I'm terrible with regular expressions but I was wondering if it was possible to use ng-pattern with a variable
例如,
ng-pattern="/^{{validationCode}}$/"
在这里验证code是连接到$范围在控制器的变量
where validationCode is a variable attached to $scope in a controller
// Inside Controller
$scope.validationCode = 'response returned from server'
如果
$scope.validationCode = 1234123412351234
那么NG-模式将
then the ng-pattern would be
ng-pattern="/^1234123412351234$/"
但是,这并不工作,好像我需要创造,我真的不希望自定义指令
But this isn't working and it seems like I need to create a custom directive which I don't really want
推荐答案
NG-模式
需要一个正则表达式的前pression。
ng-pattern
expects a regex expression.
大约从角的文档 NG-模式
:
设定模式验证错误的关键,如果该值不匹配的正则表达式
模式前pression。期望值是 /正则表达式/
内联模式或正则表达式
的定义范围前pressions模式。
Sets pattern validation error key if the value does not match the
RegExp
pattern expression. Expected value is/regexp/
for inline patterns orregexp
for patterns defined as scope expressions.
在换句话说,你可以在控制器中创建一个正则表达式:
In other words, you could create a RegExp in the controller:
$scope.pattern = new RegExp(patternFromServer);
和使用它:
<input ng-model="foo" ng-pattern="pattern">
这篇关于是有可能使用纳克图案具有可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!