【发布时间】:2015-07-06 01:12:42
【问题描述】:
如果用户单击没有内容的提交,我希望出现一条错误消息,并且我希望禁用提交按钮。 我可以让任何一个工作,但不能同时工作。
下面的代码显示消息,但允许一个空的待办事项。
<form name="todoForm" novalidate >
<div ng-messages="todoForm.new.$error" ng-if="todoForm.$submitted"><div ng-message="required">Add Your Item Below...</div></div><!--message appears until valid input is entered-->
<input type="text" name="new" placeholder="start typing..." autofocus data-ng-model="newTodo" required=""/>
<button input type="submit" ng-click="addTodo()" >Add To List</button><!--disables form if form not valid-->
</form>
此版本禁用提交按钮但不弹出消息
<form name="todoForm" novalidate >
<div ng-messages="todoForm.new.$error" ng-if="todoForm.$submitted"><div ng-message="required">Add Your Item Below...</div></div><!--message appears until valid input is entered-->
<input type="text" name="new" placeholder="start typing..." autofocus data-ng-model="newTodo" required=""/>
<button input type="submit" ng-click="addTodo()" data-ng-disabled="todoForm.$invalid" >Add To List</button>
</form>
我想这是因为输入按钮被禁用时无法显示消息,因为没有提交任何内容?
我尝试使用 $disabled 和 $invalid 来代替,但它们没有奏效。
【问题讨论】:
-
除非我遗漏了什么,否则我从中得到的是
ng-if="todoForm.$submitted"与ng-messages="todoForm.new.$error"冲突。即ng-if出现在同一个 div 上,我看不到ng-message="required"文本出现。当我删除ng-if时,显示所需的消息:plnkr.co/edit/8pE3jmBNYRpPSvyJA0ys?p=preview -
据我所知, ng-if="todoForm.$submitted" 与 data-ng-disabled="todo.Form.$invalid" 冲突。您的示例没有生成 ng 消息,那不是 html5 消息吗?
标签: angularjs forms message disabled-input