1

How can I achieve something like this:

ng-class="{ 'has-errors' : form_editProfile.password.$invalid && !form_editProfile.password.$pristine......

but, within a loop/ng-repeats (of dynamic forms, and dynamic fields), something like:

 <div ng-repeat="(form, fields) in sections">
    <form name="form_{{form | alphanumeric}}" novalidate>
      <div ng-repeat="(name, field) in fields" ng-class="{ 'has-errors' : form_{form | alphanumeric}.{field}.$invalid && !form_{form | alphanumeric}.{field}.$pristine, 'no-errors' : form_{form | alphanumeric}.{field}.$valid">

......where 'alphanumeric' is a filter that I have which converts "Contact Info" to "ContactInfo" (a valid form name) - for example.

The basic dynamicly generated form/form elements is working, I just can't work out how to get the validation working properly in this situation with embeded expressions..

1
  • Can you add any Fiddle/Plnkr? Commented Apr 7, 2016 at 5:42

1 Answer 1

2
+50

Simple interpolation should work here, like this:

ng-class="{ 'has-errors' : form_{{form | alphanumeric}}.{{field}}.$invalid }"

Here's a quick demo showing how the form name and input name can both be interpolated in a ng-class rule.

angular.module('myApp', [])
  .controller('MainCtrl', function($scope) {
    $scope.formName = 'TestForm';
    $scope.inputName = 'myInput';
  })
  .filter('lowercase', function() {
    return function(input) {
      return input.toLowerCase();
    }
  });
.red {
  color: red;
}
<section ng-app="myApp">
  <div ng-controller="MainCtrl">
    <form name="form_{{ formName | lowercase }}">
      <input type="text" name="{{ inputName }}" ng-model="inputValue" />
    </form>

    <span ng-class="{ 'red': form_{{formName | lowercase}}.{{inputName}}.$dirty }">
      This text will be red if the input is dirty
    </span>
  </div>
</section>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.