Reuse ngClass directive in AngularJS -
i trying validate form , there way logic in html page. using ngmessages because it's quite hopeless without it.
i doing object variant of ng-class this
ng-class="{ 'has-error' : registerform.username.$invalid && registerform.username.$touched, 'has-success' : registerform.username.$valid && registerform.username.$touched }" as can see there alot of code in above line. have use directive 6 times option seems right copy paste 5 other places. tried doing did not work
<form ng-init="formgroupclassobject = { 'has-error' : registerform.username.$invalid && registerform.username.$touched, 'has-success' : registerform.username.$valid && registerform.username.$touched }"> <div class="form-group" ng-class="formgroupclassobject"></div> i don't understand why not work. have other suggestions on reuse-ability of code above?
how about:
ng-class="getformclasses(registerform)" and in common controller:
$scope.getformclasses = function(form) { if (!form) { return; } return { 'has-error' : form.username.$invalid && form.username.$touched, 'has-success': form.username.$valid && form.username.$touched } }; if use ng-init, classes not updated when form validity changes.
Comments
Post a Comment