如何冻结角度分量?

时间:2017-10-20 08:43:57

标签: angular

我想要冻结角度分量。组件的所有视图侦听器。

例如:{{datas.title}}

如果标题已更改,则视图必须保持不变,并在冻结期间忽略更改。

如何冻结组件?

谢谢:)

2 个答案:

答案 0 :(得分:2)

您可以在ChangeDetectorRef上调用detach(),从自动更改检测中排除该组件:

@Component({
  selector: 'my-component',
  template: `
    {{datas.title}}
  `
})
class MyComponent {
  constructor(private changeDetector: ChangeDetectorRef) {    
  }

  freeze() {
    this.changeDetector.detach();
  }
}

答案 1 :(得分:1)

@Component({
  selector: 'my-component',
  template: `{{datas.title}}`
})
class MyComponent {
  constructor(private changeDetector: ChangeDetectorRef) {    
  }

  freeze() {
    this.changeDetector.detach();
  }
  melt() {
    this.changeDetector.reattach();
  }
}

官方文档:https://angular.io/api/core/ChangeDetectorRef