-
Notifications
You must be signed in to change notification settings - Fork 27.4k
One-way bindings + shallow watching #14039
Comments
I was recently looking into this and wondering if this could be done by changing how |
Can we probably somehow reuse the array returned from the filter when the identity of the expression result doesn't change? |
Yes, this came up in the issue after we decided on the simple implementation. It was suggested that shallow watching had been introduced exactly for that purpose, so imo we can port it to one-way bindings. A general solution be interesting too. |
I think I have another use case for the need of shallow watching objects for one way bindings. For example when using moment.js, and passing a moment object as a binding to some component, it will not update if the moment date is changed externally, because the object reference doesn't change. |
Another use case is binding a "user" object to a certain component, which uses several of the user's properties to create an avatar, for example their initials, color, profile photo, etc. If any of those properties changes externally, the changes need to be reflected in the component. However, the component itself doesn't need to make changes to the object. Hence the need for a one way binding with a shallow watch. |
A third use case is when watching an array. When adding elements to or removing elements from an array which is bound to a component via one-way binding, the |
Would it be acceptable to implement |
That will work for arrays, but still not for objects right? Is there a technical difficulty with implementing a shallow watch for one way bindings? I use moments a lot for a very date & time operations heavy app, and not being able to detect shallow changes on the moment object is holding me back from embracing the new component style of development. |
It sould work for shallow comparison of objects too. See https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watchCollection |
Sorry you're right, I got confused yesterday. Yes the equivalent of a |
Hi @petebacondarwin, any idea in which Angular version this feature will land? |
Week have now implemented |
Unless I am understanding it wrong, it seems that using For example, to watch if array elements changed, I would have to introduce the following into my code:
A shallow watch would make this much simpler as it would simply call I currently already have work arounds for the above use cases in place, so it seems Is it possible to still implement |
I agree that it would not be much work. My only concern is that it is deviating from the way that Angular 2 does things, which is not ideal... What would you do in Angular 2 for these use cases? |
I don't know, good question -- I haven't had to handle that yet in an Angular 2 project. But my argument for Angular 1 would be that if Angular already has the mechanics and internal logic in place to be able to shallow watch for changes, then it would probably be more efficient if we can hook in to those tools and use them rather than try to hack our own solution. Moreover, there's much more stuff in Angular 1 that isn't present in Angular 2, and Angular 1 started out as a very two-way binding oriented framework. I feel it would be a wrong approach to avoid adding features that make Angular 1 development easier, just because Angular 2 doesn't have something similar. The whole development methodology in Angular 2 in general is quite different, using mostly one-way reactive data flow, observables, classes, types etc. So while I appreciate you want to make the migration path easier, I think there's no need to strive for a 100% one-to-one feature mapping, because of these differences. I'll try to play around with it in Angular 2 next week and see what I come up with. |
Any updates? |
We might consider it (given #15874 (comment)). |
Awesome, thanks team! 🎉 |
How can I use it in controller binding? @Narretz |
When the PR #13928 (one-way bindings) was discussed, the shallow watching (using
$watchCollection
) wasn't implemented as it wasn't clear what the use case is. Seems like I've found a use case.Suppose we have a component:
and it's used like this:
Can you see what's going on here? The
orderBy
filter returns a new array every time it's called, so we get$rootScope:infdig
. Plunker.Changing the type of the binding to
=*
makes it work.The text was updated successfully, but these errors were encountered: