2016-01-20 42 views
4

我有一個ng-repeat屬性列表,並且想要顯示每個輸入的錯誤消息。對於模式錯誤,我也想顯示它必須匹配的特定正則表達式。訪問錯誤消息中的ng-pattern

我可以以某種方式訪問​​輸入模式嗎?我知道我可以添加一個包含正則表達式的屬性,但我想知道是否有某種角度的方式。

(這是一個過於簡單的例子,該正則表達式可以針對不同屬性不同)

<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/

+0

你想要HH:MM這樣的東西顯示出來嗎,還是你在'data-mg-pattern'中設置的實際正則表達式模式? – frishi

+0

正則表達式模式會很好。這是一個相當技術的觀衆,應該保持高度通用。 –

+1

在這種情況下,將模式存儲在範圍變量中並將其顯示在錯誤消息中,你不是更好嗎? '$ scope.timeRegexPattern ='/^[0-9] [0-9]:[0-9] [0-9] $ /'' – frishi

回答

2

根據您的限制,該模型無法改變,我想你最好的選擇是有一個單獨的模型,只包含正則表達式