我有一个奇怪的问题。
在我的组件 CategoryComponent 中,我在 escope 中有一个变量,即:
@Input() 类别:类别模型;
当我使用服务发布一个简单的帖子并返回一个数据时,我会更改此变量以进行更改,因此我创建了一个执行此操作的函数:
if (category.uploading) {
this.checkMedia(this.category, this.module);
}
在我的函数中,我这样做:
checkMedia(model: any, module: String) {
let counter = 0;
var refreshIntervalId = setInterval(() => {
const service =
model instanceof CategoryModel
? this.categoryService
: this.foodService;
service
.hasMedia(model)
.pipe(
switchMap((hasMedia) => {
if (hasMedia) {
return service.show(model, module);
}
return of(undefined);
}),
catchError((_) => {
clearInterval(refreshIntervalId);
return of(undefined);
})
)
.subscribe(
(newModel) => {
if (newModel) {
if(newModel.hasMedia) {
model.hasMedia = newModel.hasMedia;
model.images = newModel.images;
model.uploading = false;
console.log(model);
console.log(this.category);
clearInterval(refreshIntervalId);
this.cd.detectChanges();
}
}
},
(_) => {
clearInterval(refreshIntervalId);
}
);
counter++;
if (counter == 3) {
if (refreshIntervalId) {
model.uploading = false;
clearInterval(refreshIntervalId);
this.cd.detectChanges();
}
}
}, 8000);
}
为什么在这部分:
model.hasMedia = newModel.hasMedia;
model.images = newModel.images;
model.uploading = false;
console.log(model);
console.log(this.category);
clearInterval(refreshIntervalId);
this.cd.detectChanges();
仅当我直接引用此变量时,我的屏幕才会更改,例如this.category.name = "TESTE"; ,例如,但是当我使用参数model:any时,当我在 console.log() 中看到它时,它会更改全局变量,但在我的首页中,我想要的状态没有改变,为什么?