У меня есть форма с именем, которое передается контроллеру, а когда форма $ valid, я продолжаю обрабатывать данные. Это использовалось для работы с переключателями, но когда я переключался на флажки, проверка проходит только тогда, когда выбраны все флажки. Как я могу это сделать, выбор 1 делает форму действительной? Код фрагмента для справки:
<form name="snippetForm" ng-submit="vm.submitForm(snippetForm)" novalidate>
<input type="checkbox" ng-model="vm.destination.1" ng-true-value="'1'" name="location" required >
<input type="checkbox" ng-model="vm.destination.2" ng-true-value="'2'" name="location" required >
<input type="checkbox" ng-model="vm.destination.3" ng-true-value="'3'" name="location" required >
</form>
vm.submitForm = function(form) {
if(form.$valid) {
console.log('Passes only when all checkboxes from form.location are selected')
}
}
Я добавил выражение ng-change
в каждый флажок. Выражение будет неверным, если флажок не установлен.
<form name="snippetForm" ng-submit="vm.submitForm(snippetForm)" novalidate>
<input type="checkbox" ng-model="vm.destination1" ng-true-value="'1'" name="location" ng-change="{{(!vm.destination1 && !vm.destination2 && !vm.destination3) ? (snippetForm.$valid = false) : (snippetForm.$valid = snippetForm.$valid)}}" >
<input type="checkbox" ng-model="vm.destination2" ng-true-value="'2'" name="location" ng-change="{{(!vm.destination1 && !vm.destination2 && !vm.destination3) ? (snippetForm.$valid = false) : (snippetForm.$valid = snippetForm.$valid)}}" >
<input type="checkbox" ng-model="vm.destination3" ng-true-value="'3'" name="location" ng-change="{{(!vm.destination1 && !vm.destination2 && !vm.destination3) ? (snippetForm.$valid = false) : (snippetForm.$valid = snippetForm.$valid)}}" >
</form>