我从AngularJs 2应用程序,API返回数据调用WebAPI,并且我将它分配给我的Angular组件的属性但是当我使用HTML时它的这个属性时,它说的是undefiend。请帮忙
错误消息
<ion-view>
<ion-content class="has-footer">
...
...
</ion-content>
<div class="tabs tabs-icon-top footer_tab" ng-include="'templates/footer.html'" ></div>
</ion-view>
以下是我的benefit.component.ts代码
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'BenefitId' of undefined
HTML代码
import { Component, OnInit, ViewChild } from '@angular/core';
import { BenefitService } from '../services/benefit.service';
import { IBenefit } from '../models/benefit';
@Component({
templateUrl: 'app/Components/benefit.component.html'
})
export class BenefitComponent{
constructor(private _benefitService: BenefitService) { }
benefits: any;
msg: string;
ngOnInit(): void {
this.LoadUsers();
}
LoadUsers(): void {
this._benefitService.get("http://localhost/MyApi/api/Get/1950/7775/1")
.subscribe(benefits => { this.benefits = benefits; console.log(this.benefits); console.log('hi'); },
error => this.msg = <any>error);
}
}
答案 0 :(得分:0)
优势为undefined
,直到API返回数据,当然您无法获取BenefitId
的属性undefined
。
所以,试试吧:
ngOnInit(): void {
this.benefits = {};
this.LoadUsers();
}