Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2+ one-time binding

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?

like image 370
Miquel Avatar asked Dec 19 '15 21:12

Miquel


People also ask

Does Angular support 2 way binding?

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.

What is 2 way data binding 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.

Is Angular one-way binding?

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.

Does angular2 support 2 way binding?

Angular v2+ supports two-way data binding using ngModel directive and also by having getter and setter methods.


Video Answer


1 Answers

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> 
like image 58
M Barzel Avatar answered Oct 08 '22 00:10

M Barzel