Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以将ng模式与变量一起使用_Javascript_Regex_Angularjs - Fatal编程技术网

Javascript 是否可以将ng模式与变量一起使用

Javascript 是否可以将ng模式与变量一起使用,javascript,regex,angularjs,Javascript,Regex,Angularjs,我对正则表达式很在行,但我想知道是否可以将ng模式与变量一起使用 比如说, ng-pattern="/^{{validationCode}}$/" 其中validationCode是附加到控制器中$scope的变量 // Inside Controller $scope.validationCode = 'response returned from server' 如果 那么ng模式将是 ng-pattern="/^1234123412351234$/" 但这不起作用,似乎我需要创建一个

我对正则表达式很在行,但我想知道是否可以将ng模式与变量一起使用

比如说,

ng-pattern="/^{{validationCode}}$/"
其中validationCode是附加到控制器中$scope的变量

// Inside Controller
$scope.validationCode = 'response returned from server'
如果

那么ng模式将是

ng-pattern="/^1234123412351234$/"

但这不起作用,似乎我需要创建一个自定义指令,我并不真正想要它

ng模式
需要一个正则表达式

从Angular's about
ng模式

如果值与
RegExp
模式表达式不匹配,则设置模式验证错误键。对于内联模式,预期值为
/regexp/
,对于定义为范围表达式的模式,预期值为
regexp

换句话说,您可以在控制器中创建一个RegExp:

$scope.pattern = new RegExp(patternFromServer);
并使用它:

<input ng-model="foo" ng-pattern="pattern">

哦……你知道吗。。。我想我发现了一个类似的问题…废话
<input ng-model="foo" ng-pattern="pattern">