supertonsky
supertonsky

Reputation: 2733

How do I access form from ng-form if the form name is a variable?

The following code works if formName is set to "myformName"

<ng-form="{{formName}}">
   is form dirty? : {{myformName.$dirty}}
   entire form object : {{myformName | json}}
</ng-form>

But of course we don't know the form name because we're using a variable.

How do I access form from ng-form using a variable form name and not using a hardcoded form name?

The following doesn't work:

<ng-form="{{formName}}">
   is form dirty? : {{myScope[myScope.formName].$dirty}}
   entire form object : {{myScope[myScope.formName] | json}}
</ng-form>

Upvotes: 3

Views: 87

Answers (1)

Phil
Phil

Reputation: 164767

In AngularJS templates, the current $scope may be referenced by this. With that in mind, you can use something like

{{this[formName].$dirty}}

Upvotes: 3

Related Questions