gpt4 book ai didi

javascript - 将 ng-message 与 ng-required 一起使用

转载 作者:行者123 更新时间:2023-12-03 07:04:30 28 4
gpt4 key购买 nike

我有一个验证 radio 输入的表单。需要单选输入(用户必须在单击提交按钮之前选择单选字段之一)。我在输入 radio 中使用 ng-required。

radio 输入看起来像这样:

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

关于javascript - 将 ng-message 与 ng-required 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36886671/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com