オブジェクトを使用して、コンポーネントに表示されるデータを定義します。 HTTPサービス経由で読み込まれる今のJSONファイルからのデータで配管し、このエラーを取得: Angular2 + "プロパティXを未定義に読み込めません"
、それはここにある私のメインコンポーネント内で失敗しているように見えます:
import { Component, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Contribution } from '../contribution';
import { ContribService } from '../contrib.service';
import { Card } from '../card';
import { Group } from '../group';
@Component({
providers: [ContribService],
selector: 'main',
templateUrl: './main.component.html',
styleUrls: ['./main.component.css']
})
export class MainComponent implements OnInit{
contrib:Contribution = new Contribution();
constructor(private contribService: ContribService){
}
ngOnInit(){
this.contribService.getContribution().subscribe(data=>this.contrib = data);
}
maskCard: boolean = true;
unmaskedCard: string = this.contrib.thisCard.number;
maskedCard: string = 'XXXXXXXXXXXX' + this.unmaskedCard.substring(11,15);
displayedCard: string = this.maskedCard;
maskToggle = function(): void{
if(this.maskCard == true){
this.displayedCard = this.maskedCard;
}else{
this.displayedCard = this.unmaskedCard;
}
}
}
私が理解しているところでは、エラーはオブジェクト "thisCard"の問題以上の可能性があります。
どうすれば回避できますか?
の可能性のある重複(https://stackoverflow.com/questions/43055706:データが解決された初期化を移動してみてください[私はangular2において観察/ HTTP /非同期呼び出しからの応答を返すにはどうしますか?] /どのようにdo-i-return-the-observable-http-async-call-in-angular2からの応答2) – Alex