【发布时间】:2018-10-15 10:12:41
【问题描述】:
我的系统有问题。
基本上,我有一个实体:
import { Produto } from "./produto.model";
import { Empresa } from "./empresa.model";
import { Mensagem } from "./mensagem.model";
export class Nfe{
public codNfe: number;
public numeroNfe: number;
public dataEmissao: Date;
public valorTotal: number;
public empresa: Empresa;
public produtos: Array<Produto>;
public mensagens: Mensagem;
constructor(){};
}
还有一项服务:
constructor(private http: Http) { }
public getNFE(numero: string): Promise<Nfe>{
return this.http.get(`${URL_API}nfe?numeroNfe=${numero}`)
.toPromise()
.then((response: any) => {
return response.json()
});
}
还有一个观点:
export class ExibirNfeComponent implements OnInit {
public nfe: Nfe;
constructor(private service: NfeService) {
}
ngOnInit() {
this.service.getNFE('26180406057223028939650050000133071050336085')
.then((nfe: Nfe) => {
this.nfe = nfe;
})
}
}
<div class="row">
<div class="col-sm-2">Id: {{nfe.codNfe}}</div>
</div>
当我调用我的屏幕时,我收到此错误:
ERROR TypeError: Cannot read property 'codNfe' of undefined
我认为发生这种情况是因为服务需要一些时间来响应。我需要一种方法让视图等待我的对象变满。
我该怎么做?
更新 1
我放了一些控制台日志:
ngOnInit() {
console.log('Begin OnInit')
console.log(this.nfe)
this.service.getNFE('26180406057223028939650050000133071050336085')
.then((nfe: Nfe) => {
this.nfe = nfe;
console.log(this.nfe)
});
console.log('End OnInit');
}
【问题讨论】:
标签: angular promise angular-promise