encuadernación angular 2 una vez


En angular 1 podríamos hacer un enlace de una vez de esta manera: {{ ::myFunction() }}.

En angular 2 esto es lanzar:

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>

¿Cómo podemos hacer un enlace de una vez en angular2?

Author: Miquel, 2015-12-20

4 answers

 14
Author: Miquel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-31 17:35:37

He encontrado la solución para un enlace de tiempo en angular 2 aquí: https://github.com/angular/angular/issues/14033

Creé esta directiva:

 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());
        })
    }
}

Y lo usó:

  <some-selector *oneTime [somePropertyToOneTimeBinding]="someValueToOneTimeBinding"></some-selector>

Por ejemplo:

     <iframe *oneTime [src]="myUrl"></iframe>
 6
Author: m e,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-18 19:03:22

Actualmente, no se puede hacer un enlace único con Angular 2. Sin embargo, puede saber cuándo cambia su enlace y restablecer sus entradas.

Angular 2 proporciona un gancho de ciclo de vida de onChanges para el mismo. Necesita implementar la interfaz onChanges para obtener los cambios.

Vea el ejemplo de código a continuación donde, estoy almacenando la propiedad data-bound en una variable privada cuando se llama a OnInit.

export class Footer implements OnInit, OnChanges {
  @Input() public name: string;
  private privname: string;

  constructor() { }

  ngOnInit() {
    this.privname = this.name;
  }


  ngOnChanges(changes: { [key: string]: SimpleChange }): void {
    if (!changes["name"].isFirstChange()) {
        this.name = this.privname;
    }
  }
}

Más tarde, cuando se producen otros cambios, estoy configurando el valor a su valor antiguo en cambios posteriores.

Este mecanismo funciona como enlace.

Soluciones Alternativas: También puede usar una función setter para capturar los cambios.

 5
Author: Jigar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-29 20:39:53

ChangeDetectionStrategy.CheckOnce es la solución para este problema.

Esto se ha actualizado a OnPush ver también comentario en el código:

export declare enum ChangeDetectionStrategy {
    /**
     * `OnPush` means that the change detector's mode will be set to `CheckOnce` during hydration.
     */
    OnPush = 0,
    /**
     * `Default` means that the change detector's mode will be set to `CheckAlways` during hydration.
     */
    Default = 1,
}
 3
Author: eav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-07 09:08:43