【发布时间】:2021-10-03 19:33:39
【问题描述】:
一开始,我使用了一个服务,它给了我一个字符串,我将它保存在变量中,并使用大括号在 HTML 上显示它,但我没有看到值,我已经测试了服务和值来自后端,所以我认为这种情况是由于生命周期。我尝试了ngOnInit 和ngAfterViewInit,但我无法获得价值。
elCodigoInterno: string;
ngOnInit() {
this.iniciarFormulario();
this.dataService.getCodigoInterno().subscribe(
//result => this.colaborador.codigoInterno = result,
result => this.elCodigoInterno = result,
error => console.log('error ', error)
);
}
ngAfterViewInit(){
this.dataService.getCodigoInterno().subscribe(
//result => this.colaborador.codigoInterno = result,
result => this.elCodigoInterno = result,
error => console.log('error ', error)
);
}
<label for="codigoInterno">Código Interno </label>
<p>{{elCodigoInterno}} </p>
【问题讨论】: