Two-way binding between a directive and a parent scope is delayed by one cycle.

1,723 views
Skip to first unread message

Dan Rybij

unread,
Aug 2, 2012, 9:11:29 AM8/2/12
to ang...@googlegroups.com
Angular 1.0.1.

I'm trying to make a re-usable component directive using bi-directional binding using the

    scope : { localVar : '=boundAttribute' }

pattern in a directive. When a local value changes, the scope.$watch listener fires at the correct time with the correct newVal and I assign that newVal to scope.localVar, hoping it will update the bound value in the parent. But the parent scope applies after TWO changes to the local scope. If I try to scope.$apply() in my listener function, the parent scope updates immediately, but then I get a "$digest already in progress" error.

A fiddle of a minimal example is here: http://jsfiddle.net/HK84N/2/

Thank you.

BTW - I love Angular and it's letting me quickly craft a rather large, robust intranet application!

Pawel Kozlowski

unread,
Aug 2, 2012, 2:38:26 PM8/2/12
to ang...@googlegroups.com
Hi Dan!

I'm wondering if you are trying to learn how to write directives or
are you simply after covering your use-case? I'm asking since what you
are trying to do can be easily done without any directives. Here is
just one of possible solutions:
http://jsfiddle.net/pkozlowski_opensource/qa5mN/2/

Cheers,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en.
>
>

Eddie Huang

unread,
Aug 2, 2012, 2:46:24 PM8/2/12
to ang...@googlegroups.com
I think you found a bug, for some reason the '=' watcher from angular does not trigger dirty to true, so all other watchers are not triggered when parent scope is updated

E_lexy

unread,
Aug 2, 2012, 2:55:22 PM8/2/12
to ang...@googlegroups.com
I think this is the same thing I encountered. is it a bug?

E_lexy

unread,
Aug 2, 2012, 3:00:48 PM8/2/12
to ang...@googlegroups.com
Another discussion about the same subject: https://groups.google.com/forum/?fromgroups#!topic/angular/Ftcme6FNFlY

Dan Rybij

unread,
Aug 2, 2012, 4:12:44 PM8/2/12
to ang...@googlegroups.com
YES - I am trying to learn how to write directives. My actual situation is quite a bit more complicated than my example so I would like the code re-usable as a component. I thought a directive with two-way binding was the way to go, so I used '=' binding in the directive. But when I learned that the '=' binding doesn't work as I expected I grew concerned and thought I'd ask the group if this is indeed a bug or if I'm doing something wrong.

Since my earlier post, I worked around the problem by referencing scope.$parent[attrs.myModel] in the linking function. That seems to get /set the parent scope as expected. But then the '=' binding is not required.

So does the '=' method of binding not working properly? Or am I doing something wrong?
> angular+unsubscribe@googlegroups.com.

Eddie Huang

unread,
Aug 2, 2012, 4:44:52 PM8/2/12
to ang...@googlegroups.com
You can Open a bug report at https://github.com/angular/angular.js/issues?state=open

Isolated scope gut issues... Here another kind of problem I've hit https://github.com/angular/angular.js/issues/1210

Josh Iverson

unread,
Aug 3, 2012, 6:52:12 PM8/3/12
to ang...@googlegroups.com
Yeah I actually talked to Igor about this issue when he was in Phoenix it does seem to be a bug.


On Thursday, August 2, 2012 6:11:29 AM UTC-7, Dan Rybij wrote:

k...@halftreelabs.com

unread,
Feb 10, 2013, 12:10:35 PM2/10/13
to ang...@googlegroups.com
any updates on this?  using angular 1.1.1, still having this issue.

Jeff Nadler

unread,
Feb 21, 2013, 10:51:57 PM2/21/13
to ang...@googlegroups.com

Is there anyone on the angular team who has looked at this?   I just updated to 1.0.5 and I'm having this exact same problem.   Two way binding between directive and parent controller is delayed by one digest cycle - when I change a value in the directive, the change is not reflected in the controller scope immediately.   It is reflected when I make the *next* change.   Of course, that next change isn't reflected until the cycle after that...

zdam

unread,
Feb 21, 2013, 11:07:07 PM2/21/13
to ang...@googlegroups.com
Hi,

When i run that fiddle I am *not* seeing the problem described.  It appears to cycle through correctly for me.

What browser are you seeing the behaviour in?

Could it be related to something like this: https://github.com/angular/angular.js/issues/885

Cheers, Adam.

Jeff Nadler

unread,
Feb 22, 2013, 11:56:05 AM2/22/13
to ang...@googlegroups.com

I can reproduce this consistently in FireFox 19.   I may need to whip up a new fiddle, because I can't reproduce it using the OP's fiddle either but I can certainly reproduce it in our application.  Our bound object is rather more complex, it has nested objects with array values.  I suspect this might be related, because the two way binding into directives works fine in other places in our app, with simple bound objects.

I have a workaround in place whereby I pass in a controller function to the derivative, and whenever values change I pass the value back to the controller by calling the function and passing the object as an arg.   Not as clean obviously, but functional.

Eddie Huang

unread,
Feb 28, 2013, 2:39:19 PM2/28/13
to ang...@googlegroups.com
I ran into a bug before where ng-repeat induced change would not be detected by AngularJS. With examples.

It was closed as dupe though.
Reply all
Reply to author
Forward
0 new messages