Is it possible to use ng-pattern with a variable

后端 未结 1 1143
梦毁少年i
梦毁少年i 2021-01-01 17:33

I\'m terrible with regular expressions but I was wondering if it was possible to use ng-pattern with a variable

For example,

ng-pattern=\"/^{{valid         


        
相关标签:
1条回答
  • 2021-01-01 17:55

    ng-pattern expects a regex expression.

    From Angular's documentation about ng-pattern:

    Sets pattern validation error key if the value does not match the RegExp pattern expression. Expected value is /regexp/ for inline patterns or regexp for patterns defined as scope expressions.

    In other words, you could create a RegExp in the controller:

    $scope.pattern = new RegExp(patternFromServer);
    

    and use it:

    <input ng-model="foo" ng-pattern="pattern">
    
    0 讨论(0)
提交回复
热议问题