我正在使用角度1.1.1和Ionic構建表單。角度驗證僅適用於表單內的1個輸入字段
有許多「錢包」,用戶需要發送一個新的「價值」到每個錢包。我的表單驗證所有字段,當按下表單的「提交」按鈕時工作正常。
但是,我也有每個錢包旁邊的一個按鈕,僅向此錢包發送值(與所有錢包沒有不同的值)。當我按下它時,出現所有驗證錯誤,但是我只需要爲特定錢包顯示錯誤。
我的形式(的index.html):
<form name="myForm" ng-submit="sendValues(wallets)" ng-controller="valuesCtrl" novalidate>
<div class="row" ng-repeat="wallet in wallets">
<div class="col item item-input-inset">
<label class="item-input-wrapper item-text-wrap">
<input name="wallet_{{wallet.id}}" type="number" ng-model="wallet.value" type="text" required/>
</label>
<span ng-show="myForm.wallet_{{wallet.id}}.$error.required">!!!</span>
</div>
<div class="col item">{{ wallet.previous }}</div>
<button ng-click="sendValue(wallet)">
<i class="ion-android-send"></i>
</button>
<span class=ng-show="myForm.$submitted==true && myForm.wallet_{{wallet.id}}.$error.required">Required</span>
</div>
<button class="button" type="submit">Submit</button>
</form>
我控制器(values.js):
'Use Strict';
angular.module('App')
.controller('valuesCtrl', function($scope, $localStorage, UserService, $state) {
$scope.sendValues = function(wallets){
if ($scope.myForm.$valid) {
...
} else {
$scope.myForm.submitted = true;
}
},
$scope.sendValue = function(wallet){
if (wallet.value == null) {
$scope.myForm.submitted = true;
} else {
...
}
}
})
在這種情況下,驗證所有的錢包不工作 – Zelenka
這並不提供答案的問題。要批評或要求作者澄清,請在其帖子下方留言。 - [來自評論](/ review/low-quality-posts/13696625) – Mostafiz
@Mostafiz這是一個答案。請閱讀[本文](http://meta.stackoverflow.com/questions/287563/youre-doing-it-wrong-a-plea-for-sanity-in-the-low-quality-posts-queue)和[這篇文章](http://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is-an-answer-not-an-answer)。 –