gpt4 book ai didi

angularjs - ngMessages : how to hide or show one message after the other

转载 作者:行者123 更新时间:2023-12-01 12:38:43 24 4
gpt4 key购买 nike

我有以下代码:

<div id="messageArea">
<div ng-messages="text1.$error">
<div ng-message="required">
Text1 is required ...
</div>
</div>
<div ng-messages="text2.$error">
<div ng-message="required">
Text2 is required ...
</div>
</div>
<div ng-messages="text3.$error">
<div ng-message="required">
Text3 is required ...
</div>
</div>
</div>
<div>Text1<input id="text1" ng-model="text1" name="text1" ng-required="true"></div>
<div>Text2<input id="text2" ng-model="text2" name="text2" ng-required="true"></div>
<div>Text3<input id="text3" ng-model="text3" name="text3" ng-required="true"></div>

我想一次显示一条错误消息。解决第一个错误后,将显示下一个错误消息。任何人都知道我该怎么做?谢谢。

最佳答案

不错的解决方案是使用 CSS 和 ng-class

var app = angular.module('app', ['ngMessages']);
.error-wrapper {
color: red;
}

.error-wrapper > .view {
display: block;
}

.error-wrapper > .view ~ .view{
display: none;
}
<!DOCTYPE html>
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="https://code.angularjs.org/1.4.7/angular-messages.min.js"></script>
</head>

<body ng-app="app">
<form ng-submit="createWallet()" name='testForm' novalidate>
<div>
<input ng-model='text1' type="text" name='text1' placeholder='text1' required>
</div>
<div>
<input ng-model='text2' type="text" name='text2' placeholder='text2' required>
</div>
<div>
<input ng-model='text3' type="text" name='text3' placeholder='text3' required>
</div>

<div class="error-wrapper">
<div ng-class="{ view: testForm.text1.$invalid }" class="error" ng-messages="testForm.text1.$error">
<div ng-message='required'>Text1 required</div>
</div>
<div ng-class="{ view: testForm.text2.$invalid }" class="error" ng-messages="testForm.text2.$error">
<div ng-message='required'>Text2 required</div>
</div>
<div ng-class="{ view: testForm.text3.$invalid }" class="error" ng-messages="testForm.text3.$error">
<div ng-message='required'>Text3 required</div>
</div>
</div>
<button>Submit</button>
</form>

</body>

</html>

它只显示类 .view 的第一条错误消息,在 plunker http://plnkr.co/edit/kBSpRJLs6QA2D0jctKXB?p=preview

关于angularjs - ngMessages : how to hide or show one message after the other,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26975949/

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