簡體   English   中英

將ng-message與ng-requried一起使用

[英]Use ng-message with ng-requried

我有一個驗證單選輸入的表格。 無線電輸入是必需的(用戶必須在單擊“提交”按鈕之前選擇一個無線電字段)。 我正在使用輸入收音機中所需的ng。

無線電輸入看起來像這樣:

<input type="radio" name="myRadio" value="1" ng-model="theModel" ng-required="!theModel">
<input type="radio" name="myRadio" value="2" ng-model="theModel" ng-required="!theModel">
<input type="radio" name="myRadio" value="3" ng-model="theModel" ng-required="!theModel">

用戶提交后,我想在表單中顯示一條消息。 我為此使用ng-message指令。 我用這種方式:

<div class="msg-error-input" ng-if="!theModel" ng-messages="myForm.myRadio" role="alert">
    <div class="icon my-icon" ng-message="required">
        It has to show an error here. But this doesn't seem to work ! It seems that there's no link between the ng-message='required' and the ng-required..
    </div>
</div>

我通過以下方式解決了這一問題:

<form name="myForm" id="myForm">
<input type="radio" name="myRadio" value="1" ng-model="theModel" ng-required="!theModel">
<input type="radio" name="myRadio" value="2" ng-model="theModel" ng-required="!theModel">
<input type="radio" name="myRadio" value="3" ng-model="theModel" ng-required="!theModel">
<div class="msg-error-input" ng-if="!theModel && submitted" ng-messages="myForm.myRadio" role="alert">
    <div class="icon my-icon" ng-message="required">
        It has to show an error here. But this doesn't seem to work ! It seems that there's no link between the ng-message='required' and the ng-required..
    </div>
</div>
<button type="submit" data-ng-click="submitted"/>
</form>

(我看到提交的ng沒有添加到我的表單中,所以我添加了一個在click上提交的變量)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM