question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
shlomiassafcommented, Jan 20, 2016

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:

  • The invocation will continue still.
  • I create boilerplate by adding a private member that hold a reference to the “1st” returned value.

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:

@Input(true) private prop: string;

This create some API changes, tough not necessarily breaking but it does. So we can:

@OneTime() @Input() private prop: string;

* Provide binding based change detection API: ChangeDetectorRef can provide an API to freeze change detection for a BindingTarget. This is a more complex scenario that allows granularity.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 11, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found