angularjsangularjs-formsangularjs-templates

Expose ng-form in scope and reference in ng-init


I am attempting to do something like this:

<div ng-form="testForm" ng-init="$ctrl.doStuffWithForm(testForm)>

Where I define a form/publish the form to the current scope with the name testForm and pass that form object to $ctrl.doStuffWithForm() in the ng-init. However, what appears to be happening is that, at the time of the ng-init, the form creation and/or the creation of testForm in the scope has not happened yet (other testing indicates to me that testForm does point to a form controller at a later point in the Angular lifecycle).

Is there any way for me to pass the form controller to a method in the ng-init like this? Or should I be doing it another way? Is there an attribute similar to ng-init, but that is used later in the lifecycle? What I am trying to do is basically an ng-repeat on an element with an ng-form, and pass each form to a controller method when each element is initialized.


Solution

  • I was able to achieve my desired results by putting the ng-init in a child element of the ng-form element:

    <div ng-form="testForm">
        <div ng-init="$ctrl.doStuffWithForm(testForm)></div>
    </div>