私はAngularを初めて使っているので、私が達成しようとしていることについて自分の質問をどのように形成するのかがわかりません。角2 - データの単一の記録を見る
サービスから単一のユーザーレコードを取得しているコンポーネントがあります。それらのユーザーの詳細を自分のUIに表示する必要があります。私のコードの他の部分では、それらは常に複数のレコードになっていましたので、私は*ngFor
を使用し、データ配列をループしました。しかし、これは単なる結果に過ぎないので、私はこれをどうやって達成するかについてはあまりよく分かりません。
コンポーネント:
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { UserRecord } from '../shared/user-record.interface';
import { UserService } from '../shared/user.service';
@Component({
selector: 'app-view-record',
templateUrl: './view-record.component.html',
styleUrls: ['./view-record.component.css']
})
export class ViewRecordComponent implements OnInit {
private record: UserRecord[];
private errorMessage: any = '';
private loaded = false;
private RecordID: number; // User ID of who we are looking at
constructor(private _crudService: UserService,
private activatedRoute: ActivatedRoute) { }
ngOnInit() {
// Get the userID from the activated route
this.activatedRoute.params.subscribe((params: Params) => {
this.RecordID = params['id'];
});
// Call our service and pass the userID
this._crudService.getRecord(this.RecordID)
.then(res => {
this.record = this._crudService.record;
return this._crudService.getRecord(this.RecordID);
})
.then(res => {
console.log(this.record)
this.loaded = true;
})
.catch(err => { console.error(err); });
}
}
サービス:
getRecord(userID: number) {
const headers: Headers = new Headers({
"Authorization": this._frameworkService.getSessionInfo().token
});
return new Promise((resolve, rejects) => {
this._http.post(this.baseUrl + '/fetchRecord', { "userID": userID }, { "headers": headers })
.map(res => res.json())
.subscribe((data) => {
if (data) {
this.record = data;
}
resolve(true);
});
});
}
インタフェース:
export interface UserRecord {
RecordID: number;
QID: string;
FavoriteColor?: string;
FavoriteNumber?: number;
FavoriteActor?: string;
MetaInsertUTC: string;
MetaUpdateUTC: string;
FirstName: string;
LastName: string;
NTID: string;
}
サービス結果:私のコンポーネントのHTMLで
[
{
"RecordID":"55",
"QID":"Q00019204",
"FavoriteColor":"Blue",
"FavoriteNumber":"6",
"FavoriteActor":"Bob",
"MetaInsertUTC":"2017-06-29 18:47:01.750",
"MetaUpdateUTC":null,
"FirstName":"Jim",
"LastName":"Bobs",
"NTID":"bobby"
}
]
、私は{{record.FirstName}}
を試みたが、ViewRecordComponent.html:16 ERROR TypeError: Cannot read property 'FirstName' of undefined
のエラーが表示されています。
これはデータ結果のセットではないので、私は*ngFor
がユースケースにどのように適用されるのか分かりません。
私のコンポーネントはrecord
オブジェクトにデータを格納しているので、UIからそのデータにアクセスできるはずです。 console.log
には、すべての正しいデータポイントが表示されます。
コンポーネントのHTMLにFirstName
というユーザーをどのように参照しますか?うまくいけば、私は少なくとも正しい道にいる。
私はデータをラップする '
'を追加しようとしました。私は '.then(res => {this.loaded = true;})'を使ってcomponetサービスコールでこれをtrueに設定しました。エラーはもはやコンソールにスローされませんが、データポイントも表示されません。私が思う少し進歩。 – SBBなぜ別のプロパティを使用するのですか?ちょうどあなたの 'レコード'プロパティを確認してください。次に、ロードされたプロパティの設定/クリアについて心配する必要はありません。 – DeborahK
良い点、私はそれを変更しました。エラーがなくなり、なぜそれが表示されないのかを判断しようとしたら、結果セットを使用して遊びます。ヒントをありがとう! – SBB
関連する問題