将 ng-message 与 ng-requuried 一起使用

Use ng-message with ng-requried

本文关键字:一起 ng-requuried ng-message      更新时间:2023-09-26

我有一个验证无线电输入的表单。无线电输入是必需的(用户必须在单击提交按钮之前选择其中一个无线电字段)。我正在输入无线电中使用所需的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 消息指令。我这样使用它:

<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提交的没有添加到我的表单中,所以我在点击时添加了一个提交的变量)