angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Problems in accessing form constructor from controller scope

Open hellais opened this issue 13 years ago • 4 comments

I need to access the validity or not of a from from inside of the form controller I define via ng-controller though when I attempt to acces it, it comes out as undefined.

I believe this is because FormController only gets instanced once the form directive is processed and therefore it is not accessible when referring to it inside of the form controller scope.

Here is a fiddle that demonstrates what I mean:

http://jsfiddle.net/MmDhu/1/

What I am trying to do

I may be doing it wrong and this may be a violation of the angular.js design goals.

What I am trying to achieve is that I need to access the valid or invalid state of the form from some parent scope.

This looks something like this:

http://jsfiddle.net/R2kjU/1/

(note: this jsfiddle does not actually work and is just to illustrate the concept of what I am trying to achieve)

My actual implementation is actually a bit more complex and has much more nested scopes (I am creating a set by step wizard and dynamically generating the forms from some json therfore ng-repeat and ng-select will create child scopes)

Question: Am I doing it wrong, do you have a suggested better way of doing this?

hellais avatar Dec 13 '12 13:12 hellais

Why don't you do all this in the template? Is it really necessary to access the form from the controller? I mean something like this: http://jsfiddle.net/qV4a6/2/

gonzaloruizdevilla avatar Dec 13 '12 19:12 gonzaloruizdevilla

This is not an issue with AngularJS. It is an architecture question that should be posted on the mailing list or Stack Overflow. Regarding the question, I would suggest that you factor out your wizard form business logic into a service that gets injected into any controller that needs it. It can be injected into controllers that are within forms so that they have access to the FormController properties such as $invalid, etc.

Regarding your problem with not having access to the FormController during the initial run of the controller, this can be easily dealt with using a $watch on the property of the scope.

http://jsfiddle.net/KADDu/

Closing for now. If you feel that there is actually an issue with AngularJS then please reopen and provide a breaking unit test or at least a working (breaking) example.

petebacondarwin avatar Dec 13 '12 20:12 petebacondarwin

Pete, are you able to provide me with an example of your solution to create a service? I'm having the same issue and using a $watch is not helping. If it helps, I'm using ng-switch-when which I know creates different child scopes with each instance.

jorgeso avatar Jun 28 '13 18:06 jorgeso

I found a similar issue with ng-include. If i use ng-include and ng-form i can't access the form from controller. Working example http://plnkr.co/edit/hbJJ6txbyW9IHwa4yc8X?p=preview Example with ng-include http://plnkr.co/edit/cG6L353WVyc27zKUSYY3?p=preview

CiprianCapus avatar Apr 04 '14 14:04 CiprianCapus