问题描述
我有这个组成部分。我想在@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
始终为假,无论以后再传递字符串还是新值。
这些是输出:
这是使用rxjs值时的输出:(值已更改但从未更改,请先更改为true
解决方法
因为没有之前的值