In angular 1 we could do one time binding in this way: {{ ::myFunction() }}
.
In angular 2 this is throwing:
EXCEPTION: Template parse errors: Parser Error: Unexpected token : at column 2 in [{{ ::consent(false, undefined, box) }}] in CookieConsent@5:29 ("ull-right" href="" (click)="consent(true, $event, box)">De acuerdo</a> <span class="hidden">[ERROR ->]{{ ::consent(false, undefined, box) }}</span>
How can we do one time binding in angular2?
Two-way data binding in Angular will help users to exchange data from the component to view and from view to the component. It will help users to establish communication bi-directionally. Two-way data binding can be achieved using a ngModel directive in Angular.
The two-way data binding in Angular is used to display information to the end user and allows the end user to make changes to the underlying data using the UI. This makes a two-way connection between the view (the template) and the component class. This is similar to the two-way binding in WPF.
One-way data binding in Angular (i.e. unidirectional binding) is a way to bind data from the component to the view (DOM) or vice versa - from view to the component. It is used to display information to the end-user which automatically stays synchronized with each change of the underlying data.
Angular v2+ supports two-way data binding using ngModel directive and also by having getter and setter methods.
I found solution for one time binding in angular 2 here: https://github.com/angular/angular/issues/14033
I created this directive:
import { Directive, TemplateRef, ViewContainerRef, NgZone } from "@angular/core"; @Directive({ selector: '[oneTime]', }) export class OneTimeDirective { constructor(template: TemplateRef<any>, container: ViewContainerRef, zone: NgZone) { zone.runOutsideAngular(() => { const view = container.createEmbeddedView(template); setTimeout(() => view.detach()); }) } }
And used it:
<some-selector *oneTime [somePropertyToOneTimeBinding]="someValueToOneTimeBinding"></some-selector>
For example:
<iframe *oneTime [src]="myUrl"></iframe>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With