feat(change-detection): add bind-once support in templates
See original GitHub issue@gdi2290 and I were just troubleshooting an issue related to an observable pipe always emitting a null value because the observable reference being passed into the pipe never matched the already set observable instance. This was because the template was calling a method to return an observable, causing a new observable to be created on each lifecycle tick.
{{ someService.getObservable() | async }}
It would be nice to have the same one-time binding capability as AngularJS 1.x where bindings could be expressed as “bind once” inside of templates.
One option is to create a pipe to support this, where as soon as a non-undefined value is returned from the left-side, the once
pipe would cache and keep the reference:
{{ someService.getObservable() | once | async }}
Or we could add template sugar for this.
{{ ::someService.getObservable() | async}}
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Angular OnPush Change Detection - Avoid Common Pitfalls
Learn how OnPush works (its not only about @Input() changes), learn how to use in practice and how to avoid common Pitfalls.
Read more >Creating A Bind-Once Structural Directive In Angular 7.1.4
To start experimenting with one-time bindings, let's create an Angular Directive that will log the execution of a change-detection digest. When ...
Read more >The Last Guide For Angular Change Detection You'll Ever Need
OnPush change detection strategy by adding the changeDetection property to the component decorator metadata: @Component({ selector ...
Read more >Simplifying Angular Change Detection - Telerik
This article explains change detection strategies and optimizations to help you write highly performant Angular applications. Angular can detect ...
Read more >TemplateRef and change detection - Stack Overflow
Well, what I ended up doing is create a directive: <ng-template myDirective> Content </ng-template>. And in that directive I inject ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Retry…
This is a nice to have feature and one that will help a lot of people to fine tune performance without doing workarounds.
In my use case I want the user of the component to supply a function that will return a value and supply it in a declarative way (HTML). The function needs to run in the context of the class it is written in so the most simple approach would be to supply an
@Input
property in the component and the user will bound to it.This approach results in endless invocations of the supplied function by the change detector. I can’t prevent that. I can save a reference to the returned value inside
ngOnInit
thus making sure I have 1 value for the whole component life cycle but this does not solve the problem:As I see it, One time binding is a performance tune, it should be in the hand of the component builder to decide if it is needed or not. The user of the component should decide that as it might effect other things inside the component thus it shouldn’t be a syntax thing.
Trying to avoid adding new template syntax, there are several options:
* Setting up one time binding from metadata:
This create some API changes, tough not necessarily breaking but it does. So we can:
* Provide binding based change detection API:
ChangeDetectorRef
can provide an API tofreeze
change detection for a BindingTarget. This is a more complex scenario that allows granularity.This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.