supertonsky supertonsky - 5 months ago 10
AngularJS Question

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>

Answer

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

{{this[formName].$dirty}}