繁体   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