[英]disable button angular if $invalid
I cannot figure out why the following code isn't working. 我无法弄清楚为什么以下代码无法正常工作。 I am following the answer here and various other tutorials/answers but the button is not disabling when the fields are empty.
我正在关注此处的答案以及其他各种教程/答案,但是当字段为空时,该按钮不会处于禁用状态。
<h3>Add address</h3>
<div ng-include="'app/views/partials/form.html'"></div>
<button ng-click="ctrl.cancelAddress()">Cancel</button>
<button ng-disabled="addressForm.$invalid" class="save-btn" ng-click="ctrl.saveAddress(ctrl.name, ctrl.address, ctrl.country)">Save</button>
app/views/partials/form.html app / views / partials / form.html
<form name="addressForm" novalidate>
<input ng-model="ctrl.name" type="text" name="RecipientName" required>
<p ng-show="addressForm.RecipientName.$error.required">Username is required</p>
<input ng-model="ctrl.address" type="text" name="AddressLineOne" placeholder="Address" required>
<p ng-show="addressForm.RecipientName.$error.required">Address is required</p>
<select ng-model="ctrl.country">
<option ng-repeat="country in ctrl.countries" value="{{country.name}}">{{country.name}}</option>
<p ng-show="addressForm.addressCountry.$error.required">Username is required</p>
</select>
</form>
I'll put them as an answer for easier access. 我将它们作为更方便访问的答案。
addressForm
doesn't exist in the (outer) scope you are referencing it from. addressForm
在您引用它的(外部)范围中不存在。 It can only be referenced within the <form>
element where it exists. 它只能在存在的
<form>
元素内引用。 You need to put those controls inside the <form>
anywhere for them to work. 您需要将这些控件放在
<form>
内的任何位置,以使其起作用。
Is your form aware of whether or not you are editing? 您的表单是否知道您是否正在编辑? Because if so, you can move those buttons into the form, and then use
ng-if="ctrl.isEditing"
. 因为这样的话,您可以将这些按钮移到表单中,然后使用
ng-if="ctrl.isEditing"
。 Of course, ctrl.isEditing
would be replaced with whatever method you have of knowing if there is editing available. 当然,
ctrl.isEditing
将替换为您知道的任何可用编辑方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.