Javascript Ionic 5-我需要在从子页面传递到父页面后检测对数组的更改
我有一个父组件,通过查询访问REST API将其保存在数组中,然后将所有数据加载到列表中,到目前为止一切正常,问题是当我转到子路由以显示特定数据的信息时,当我修改或删除数组中的项并返回到上一个路径时,列表没有更新,我必须手动刷新以查看更改,使用离子刷新内容从同一个应用程序中进行刷新,但这不是我需要的,我希望应用程序一旦重定向我,它会自动检测更改并发出get请求并重新加载列表 我的父页面的代码:Javascript Ionic 5-我需要在从子页面传递到父页面后检测对数组的更改,javascript,angular,typescript,ionic-framework,Javascript,Angular,Typescript,Ionic Framework,我有一个父组件,通过查询访问REST API将其保存在数组中,然后将所有数据加载到列表中,到目前为止一切正常,问题是当我转到子路由以显示特定数据的信息时,当我修改或删除数组中的项并返回到上一个路径时,列表没有更新,我必须手动刷新以查看更改,使用离子刷新内容从同一个应用程序中进行刷新,但这不是我需要的,我希望应用程序一旦重定向我,它会自动检测更改并发出get请求并重新加载列表 我的父页面的代码: export class ListaEnsayosPage implements OnInit {
export class ListaEnsayosPage implements OnInit {
ensayos:Ensayo[];
titulo:string="Lista de Ensayos";
constructor(private ensayoService:EnsayoService) {
};
ngOnInit() {
this.ensayoService.getAll().subscribe(data=>{
this.ensayos=data['data'];
});
};
doRefresh(event) {
setTimeout(() => {
this.ensayoService.getAll().subscribe(data=>{
this.ensayos=data['data'];
});
event.target.complete();
}, 1500);
}
}
“我的孩子”页面的代码:
export class EnsayoPage implements OnInit {
titulo:string="Info ensayo";
ensayo:Ensayo={
operador:"",
distanciaTotal:null,
radioTrayectoria:null,
materialBola:"",
carga:null,
diametroBola:null,
codigoProbeta:"",
durezaProbeta:null,
tratamientoProbeta:"",
materialProbeta:"",
observaciones:"",
};;
constructor(private activeRoute:ActivatedRoute, private ensayoService:EnsayoService) {
}
ngOnInit() {
this.activeRoute.params.subscribe(params=>{
this.ensayo.idEnsayo=params['idEnsayo'];
});
if(this.ensayo.idEnsayo){
this.ensayoService.getOne(this.ensayo.idEnsayo).subscribe(data=>{
this.ensayo=data['data'];
});
};
}
}
这是进行修改的表单的组件(出于回收代码的原因,将表单分离为一个组件)
如果我们需要查看模板,请留下github链接:
要请求角度检测更改,您可以使用
changedethetectorref
导入它
从'@angular/core'导入{ChangeDetectorRef}代码>
注射
构造函数(私有cd:ChangeDetectorRef){
}
在你需要的地方使用它
this.cd.markForCheck();
this.cd.detectChanges();
你可以阅读更多关于它的内容
formularioEnsayo: FormGroup;
@Input() ensayo: Ensayo;
constructor(private ensayoService:EnsayoService, private router:Router) {
this.formularioEnsayo = new FormGroup({
'operador': new FormControl('',Validators.required),
'observaciones': new FormControl(),
'idEnsayo': new FormControl(),
'fecha': new FormControl(),
'tiempoTotal': new FormControl(),
'distanciaTotal': new FormControl('',Validators.required),
'radioTrayectoria': new FormControl('',Validators.required),
'materialBola': new FormControl('',Validators.required),
'carga': new FormControl('', Validators.required),
'diametroBola': new FormControl('',Validators.required),
'codigoProbeta': new FormControl('',Validators.required),
'durezaProbeta': new FormControl('',Validators.required),
'tratamientoProbeta': new FormControl('', Validators.required),
'materialProbeta': new FormControl('',Validators.required)
});
}
ngOnInit() {
if(this.ensayo.idEnsayo){
this.ensayoService.getOne(this.ensayo.idEnsayo).subscribe(async data=>{
this.ensayo=data['data'];
await this.formularioEnsayo.setValue(this.ensayo);
});
}
};
altaEnsayo(){
if(this.ensayo.idEnsayo){
const key$=this.ensayo.idEnsayo
this.ensayo = this.formularioEnsayo.value;
this.ensayo.idEnsayo=key$;
this.ensayoService.change(this.ensayo,this.ensayo.idEnsayo).subscribe(data=>{
this.router.navigate(['/ensayo','lista']);
})
}else{
const data:Ensayo = this.formularioEnsayo.value;
this.ensayoService.new(data).subscribe(data=>{
this.router.navigate(['/ensayo','lista']);
});
}
}
bajaEnsayo(){
this.ensayoService.delete(this.ensayo.idEnsayo).subscribe(data=>{
this.router.navigate(['/ensayo','lista',{onSameUrlNavigation:'reload'}]);
});
}
}