我正在尝试使用ng-pattern的条件,但是它显示zip总是无效的。如果我在没有条件的情况下使用模式,那么一切都很好。
<md-input-container flex="20">
<label>Zip Code</label>
<input ng-model="$ctrl.PostalCode" name="Zip" ng-required="true"
ng-pattern="{{$ctrl.Country == 'Canada' ? '/^(\d{5}(-\d{4})?|[A-Z]\d[A-Z] *\d[A-Z]\d)$/' : '/^[0-9]*$/'}}">
<div ng-messages="$ctrl.testForm.Zip.$error">
<div ng-message="pattern">Zip Code is not valid.</div>
</div>
</md-input-container>发布于 2020-10-08 00:58:43
如果您在呈现的HTML中检查您的模式,您可以看到问题所在。\d中的\将被忽略,因为您无法对其进行转义。试试这个:
ng-pattern="{{$ctrl.Country == 'Canada' ? '^(\\d{5}(-\\d{4})?|[A-Z]\\d[A-Z] *\\d[A-Z]\\d)$' : '^[0-9]*$'}}"https://stackoverflow.com/questions/64244479
复制相似问题