【发布时间】:2021-01-24 14:21:04
【问题描述】:
我有这个组件。我想在@Input 第一次更改时做点什么。问题是changes.firstChange 始终为假(在初始化时和更改后)。
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'test',
templateUrl: './t.component.html',
styleUrls: ['./t.component.css']
})
export class TComponent implements OnChanges {
@Input()
myValue: string;
ngOnChanges(changes: SimpleChanges) {
console.log('myValue: ', changes.myValue.isFirstChange()); // always false
console.log('myValue: ', changes);
}
}
为了使用它,我正在做:
myValue = of('1').pipe(
delay(2000),
// startWith('2')
)
我也试过了:
myValue = '1'
<test [myValue]="myValue | async "></test> // 当使用of时
正如您在图片中看到的那样,changes.firstChange 始终为 false,无论我稍后传递的是字符串还是新值。
这是使用 rxjs 值时的输出:(value changed but never changed firstChange to true
【问题讨论】:
标签: angular ngonchanges