Ari*_*ael 5 observable angular2-changedetection angular
假设我必须跟随实体
export class Photo {
public data: Blob;
public User: string;
}
Run Code Online (Sandbox Code Playgroud)
以下组件
@Component({
template: '<my-other-component [(ngModel)]="photo.data"></my-other-component>'
})
export class MyComponent {
@Input()
photo: Photo;
constructor(private photoService: PhotoService, private cd: ChangeDetectorRef){}
public deletePhoto(): void {
this.photoService.deletePhoto(this.photo)
.subscribe((result: ApiResult) => {
//if done here, change detection doesn't work
this.photo = new Photo();
});
//if done here, change detection works
this.photo = new Photo();
}
}
Run Code Online (Sandbox Code Playgroud)
PhotoService 是一个发出http 删除请求的服务。
my-other-component实施ControlValueAccessor
my-other-component每当我更改在方法中绑定到子组件的对象时subscribe,都不会传播任何更改。
如果我在我的方法之外调用它,subscribe它就会起作用并my-other-component收到有关更改的通知。
photo我是否设置NULL为新实例或仅更改属性并不重要data。
我认为这种行为是设计使然,但我想知道为什么、如何以及是否可以让它发挥作用。或者也许我完全走错了路。
我已经尝试过ChangeDetectorRefandmarkForCheck
有任何想法吗?
编辑
一如既往;)发布后不久我找到了解决方案。使用ChangeDetectorRef但不是方法markForCheck。正确的是detectChanges
更改不会传播,但问题仍然存在。
为什么我必须在可观察对象中手动触发更改检测?
| 归档时间: |
|
| 查看次数: |
1642 次 |
| 最近记录: |