我在ng-if
指令中有一個表單。我想用$valid
檢查控制器中的表單驗證。AngularJs:在ng中形成 - 如果不能從控制器訪問
<div ng-if="paymentMethod == 12">
<form name="creditForm" id="cc-form" novalidate>
<div class="form-group">
<label for="cardNumber">Card Number</label>
<input type="text" autofocus class="form-control" name="card_number" ng-minlength="16" id="cardNumber" ng-model="creditCardNumber" required>
<div class="red-text" ng-messages="creditForm.card_number.$error" ng-if="creditForm.card_number.$dirty || creditForm.$submitted">
<div ng-message="required">##global.Card_Num_Required##</div>
<div ng-message="maxlength">##global.Card_Num_MinLength##</div>
<div ng-message="minlength">##global.Card_Num_MaxLength##</div>
<div ng-message="minlength">##global.Card_Num_NumeriC##</div>
</div>
</div>
,並嘗試檢查有效形式控制器
if ($scope.$parent.creditForm.$valid) {
alert('valid');
} else {
alert('not valid');
}
但形式不是從控制器訪問。
樣本plunker嘗試刪除'$ parent'? – Icycool