angularjs - Can I re-factor Angular JS validation conditionals to streamline code? -
the below code 1 row of form.
i replicate "addcarform.ucarname.$dirty && addcarform.ucarname.$invalid" several times throughout snippet. can refactor variable, updated answer conditional user's input changes/ better way of refactoring?
<div class="form-group required " ng-class="{'has-error': addcarform.ucarname.$dirty && addcarform.ucarname.$invalid}"> <label class=" control-label col-lg-5 " for="ucarname">name:</label> <div class="col-lg-7 "> <input type="text" ng-model="cardetails.carname" name="ucarname" class="form-control" required /> </div> <div class="row"> <div ng-show="addcarform.ucarname.$dirty && addcarform.ucarname.$invalid" class="error-msg col-lg-offset-5 col-lg-7"> <span ng-show="addcarform.ucarname.$error.required"> car name required </span> </div> </div> </div>
on controller, have expression return function, i.e.:
$scope.shouldshow = function() { return $scope.addcarform.ucarname.$dirty && $scope.addcarform.ucarname.$invalid; }
and do:
ng-show="shouldshow();"
just need make sure form on scope.
Comments
Post a Comment