angular-ui/angular-ui-OLDREPO

Form in controller undefined when using ui-if

zwack opened this issue · 4 comments

I want to access a form in my controller. The form lives in a DOM area which gets injected via the ui-if directive. The form can be accessed in the template but not in the controller.

See this plunker which shows a normal working form first and using ui-if around the second one.

http://plnkr.co/edit/ICUP5n9a8WqhRQlaIRG2

Dean
thanks for the hint! Apparently you can't access the child scopes from the parent: http://stackoverflow.com/questions/13428042/angularjs-access-to-child-scope

I tried one workaround which is working in this plunker: http://plnkr.co/edit/RwSxsFVUnTPgcZxNtUBc

If I'm right this introduces a third scope (MainCtrl scope > ui-if created scope > SecondCtrl scope) which allows access to the form. However I'm not sure if this can be adopted nicely in my app which uses routing and controller binding through ng-view. Will try.

Would it be possible, if you knew in advance what the child scopes need to know about their parents, to use $scope.$broadcast to send the needed data down the chain to the children?

Ok not including the form in the ui-if works as expected.
I also tried redefining the Controller on ui-if level which works. This seems to set this Controller for the newly created scope however I'm not sure if this is ok or introduces other problems.
http://plnkr.co/edit/Sw081HS5mL4k7T4wjqFF

Anyways I think the cause of the issue and how to avoid it is clear. A good idea would possibly be to include some information in the docs.

ui-if landed as ng-if in Angular 1.2 - if you really need the functionality, I recommend grabbing it from the angular.js master