Directive controller constructor being called twice?

5,180 views
Skip to first unread message

ItsLeeOwen

unread,
Apr 30, 2012, 5:13:39 PM4/30/12
to ang...@googlegroups.com
http://jsfiddle.net/ItsLeeOwen/AsD7E/

If I set the directive's controller with an ng-controller tag, it's only called once as expected.  By setting the controller in the directive it is called twice.  Is this a bug, or am I doing something wrong?

thanks

Dan Doyon

unread,
Apr 30, 2012, 5:29:47 PM4/30/12
to ang...@googlegroups.com
You probably shdnt be referencing controller on outside of directive

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/LY0QupX7pecJ.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

ItsLeeOwen

unread,
Apr 30, 2012, 6:00:13 PM4/30/12
to ang...@googlegroups.com
It doesn't make a difference whether I use a controller static variable, or a global variable to count the number of times the constructors are called.


Dan Doyon

unread,
Apr 30, 2012, 5:59:36 PM4/30/12
to ang...@googlegroups.com
Could be the first factory construction, try adding another widget

Sent from my iPhone

On Apr 30, 2012, at 3:00 PM, ItsLeeOwen <l...@coderebelbase.com> wrote:

It doesn't make a difference whether I use a controller static variable, or a global variable to count the number of times the constructors are called.


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/Co8ijJCWIAkJ.

ItsLeeOwen

unread,
Apr 30, 2012, 11:58:37 PM4/30/12
to ang...@googlegroups.com
What do you mean by first factory construction?


Dan Doyon

unread,
May 1, 2012, 1:00:06 PM5/1/12
to ang...@googlegroups.com
Sorry, i wasn't at my computer when i sent response. I took a look at your code more closely this morning. one of the things that threw me was the same name of the controller and directives. 

It appears that having controller: MyElement1 reference inside directives is causing the controller factory to run twice on that controller. Seems like a bug to me.

--dan



From: ItsLeeOwen <l...@coderebelbase.com>
To: ang...@googlegroups.com
Sent: Monday, April 30, 2012 8:58 PM
Subject: Re: [angular.js] Directive controller constructor being called twice?

What do you mean by first factory construction?


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/EcvdX0p6sz8J.

ItsLeeOwen

unread,
May 1, 2012, 2:29:39 PM5/1/12
to ang...@googlegroups.com, Dan Doyon
Based on the docs, it looks to me like both are valid controller assignment of an element directive.  MyElement1 firing the constructor twice was found to be the source of issues in an app under-development.

Ali Mills

unread,
May 2, 2012, 12:45:23 AM5/2/12
to ang...@googlegroups.com
I haven't dug deep, but it looks like a bug to me. Interestingly, if
you inline the template in your example instead of using templateUrl,
your constructor is only called once. Check it out:
http://jsfiddle.net/alimills/AsD7E/6/

Ali
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/angular/-/bRxHV8r94uwJ.

Ali Mills

unread,
May 2, 2012, 1:13:39 PM5/2/12
to ang...@googlegroups.com
And, yet another workaround is to simply swap the 'controller'
attribute with 'link': http://jsfiddle.net/alimills/M8wBa/

Ali

Ali Mills

unread,
May 2, 2012, 2:27:17 PM5/2/12
to ang...@googlegroups.com
Um... That workaround will only work if your directive only expects to have the three link services scope, attrs, and element injected.

Ali

ItsLeeOwen

unread,
May 2, 2012, 7:23:42 PM5/2/12
to ang...@googlegroups.com
If this is a bug, it seems fairly major since putting the controller into the template tag will not allow you to use a scope hashtag in the directive without the isolated-scope collision error.  I will try putting the template as an inline string.

multiple directives asking for isolated scope on:




Igor Minar

unread,
May 2, 2012, 8:03:26 PM5/2/12
to ang...@googlegroups.com
it was a bug and I've just fixed it:

ItsLeeOwen

unread,
May 2, 2012, 9:23:49 PM5/2/12
to ang...@googlegroups.com
Thank you Igor, much appreciated!

Reply all
Reply to author
Forward
0 new messages