mardi 20 octobre 2015

How to use angular to implement checkbox group required validation?

I have html template with the checkbox input in it.

HTML:

<input class="cb" type="checkbox" value="1" ng-model="form.cb.one"
        ng-required="showRequired" ng-change="handleClick()">one</input>
<input class="cb" type="checkbox" value="2" ng-model="form.cb.two"
       ng-required="showRequired" ng-change="handleClick()">two</input>...

JS:

$scope.showRequired=true;
$scope.form.cb = {one:false, two:true, three:false}
$scope.handleChange = function(){
$scope.showRequired = !($scope.chkCollection.one || $scope.chkCollection.two || $scope.chkCollection.three);
};

I was planning to change the ng-required attribute for the validation on click of the checkbox, but since it is a template, it may have the same variable name($scope.showRequired) used in other places on the page, which could affect other required fields. How to solve this problem? Thanks




Aucun commentaire:

Enregistrer un commentaire