Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使ChangeDetectionStrategy可由组件使用者配置_Angular_Angular Changedetection - Fatal编程技术网

Angular 使ChangeDetectionStrategy可由组件使用者配置

Angular 使ChangeDetectionStrategy可由组件使用者配置,angular,angular-changedetection,Angular,Angular Changedetection,假设UI库中有一个组件使用ChangeDetectionStrategy.Default,但希望通过Input()属性为消费者提供使用ChangeDetectionStrategy.OnPush的选项(例如,对于性能敏感的情况) 是否可以在运行时更改组件的更改检测策略?或者只能在@组件装饰器中设置它?可以从'@angular/core'注入ChangeDetectorRef,并在通过输入()属性触发的函数中调用detectChanges() // Import import { ChangeDe

假设UI库中有一个组件使用
ChangeDetectionStrategy.Default
,但希望通过
Input()
属性为消费者提供使用
ChangeDetectionStrategy.OnPush
的选项(例如,对于性能敏感的情况)


是否可以在运行时更改组件的更改检测策略?或者只能在
@组件
装饰器中设置它?

可以从'@angular/core'注入ChangeDetectorRef,并在通过输入()属性触发的函数中调用detectChanges()

// Import
import { ChangeDetectorRef } from '@angular/core';

//Constructor
constructor(private cdr: ChangeDetectorRef) { }

//Function
input() {
  this.cdr.detectChanges();
}