2角一次绑定 [英] angular 2 one time binding

查看:196
本文介绍了2角一次绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在角1我们可以做一次以这种方式绑定: {{:: myFunction的()}}

In angular 1 we could do one time binding in this way: {{ ::myFunction() }}.

在角2这是抛出:

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>

我们怎样才能做到一次在angular2结合?

How can we do one time binding in angular2?

推荐答案

目前,你不能做一次用角2。但是绑定,就可以知道你的绑定修改和重置您的输入。

Currently, you cannot do one time binding with Angular 2. However, you can know when your binding changes and reset your inputs.

2角提供OnChanges了相同的生命周期挂钩。您需要实现OnChanges接口获取的变化。

Angular 2 provides OnChanges lifecycle hook for the same. You need to implement OnChanges interface to get the changes.

请参见下面的地方,我在储存时的OnInit被称为私有变量数据绑定属性code的例子。

See the code example below where, I am storing the data-bound property in a private variable when OnInit is called.

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

}

后来,当发生其他变化,我的值设置为后续变化的旧值。

Later when other changes occur, I am setting the value to its old value on subsequent changes.

这个机制就像一次性结合

替代解决方案:
你也可以使用一个setter函数来捕获的变化。

Alternate solutions: You could also use a setter function to capture the changes.

这篇关于2角一次绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆