在错误消息中访问 ng 模式

Access ng-pattern in error message

本文关键字:ng 模式 访问 错误 消息      更新时间:2023-09-26

我有一个 ng-repeat 的属性列表,并希望为每个输入显示错误消息。对于模式错误,我还想显示它必须匹配的特定正则表达式。

我可以以某种方式访问输入的模式吗?我知道我可以添加一个包含正则表达式的属性,但我想知道是否有某种角度的方式。

(这是一个过于简化的示例,不同属性的正则表达式可能不同)

<form name="form">
    <div data-ng-repeat="(attributeName, attributeMetaData) in configuration.metaData">
        <input data-ng-model="configuration[attributeName]" type="text" name="{{attributeName}}" data-ng-pattern="/^[0-9][0-9]:[0-9][0-9]$/">
        <span data-ng-show="form[attributeName].$error.pattern && form[attributeName].$dirty">
            Please check your input format [pattern should go here]
        </span>   
    </div>
</form>

jsFiddle: http://jsfiddle.net/lisapfisterer/ndu2g0ev/

基于您对模型无法更改的限制,我想您最好的选择是拥有一个仅包含正则表达式的单独模型