Angular 无法读取属性';名字';在角度上未定义的

Angular 无法读取属性';名字';在角度上未定义的,angular,Angular,在显示时,它将人口统计数据显示为FirstName=Vinit,但同时给出错误,因为无法读取未定义的属性“FirstName” 请帮我解决这个错误。告诉我为什么会发生这个错误。非常感谢 个人名单服务 import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; // import

在显示时,它将人口统计数据显示为FirstName=Vinit,但同时给出错误,因为无法读取未定义的属性“FirstName” 请帮我解决这个错误。告诉我为什么会发生这个错误。非常感谢

个人名单服务

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
// import 'rxjs/add/operator/do';  // for debugging
export class Demographic{

  FirstName:string="";
  LastName:string="";

}
@Injectable()
export class PersonListService {

  /**
   * Creates a new NameListService with the injected HttpClient.
   * @param {HttpClient} http - The injected HttpClient.
   * @constructor
   */
  constructor(private http: HttpClient) {}

  /**
   * Returns an Observable for the HTTP GET request for the JSON resource.
   * @return {string[]} The Observable for the HTTP request.
   */
  getDemographic(): Observable<{Demographics: Demographic}>{
    console.log("Inside the get service")
    return this.http.get('app/person/person.json')

                 // .do(data => console.log('server data:', data))  // debug
                    .catch(this.handleError);

  }
private handleError (error: any) {
    // In a real world app, we might use a remote logging infrastructure
    // We'd also dig deeper into the error to get a better message
    const errMsg = (error.message) ? error.message :
      error.status ? `${error.status} - ${error.statusText}` : 'Server error';
    console.error(errMsg); // log to console instead
    return Observable.throw(errMsg);
  }
}
person.component.html

<ul>
        <li><b>Demographics</b></li>
        <li>FirstName:{{ demographics.FirstName }}</li>
</ul>
  • 人口统计
  • 名字:{{demographics.FirstName}}

Do
{{{demographics?.FirstName}}
。由于
demographics
是异步加载的,因此应该使用安全导航操作符
(?)

Do
{{demographics?.FirstName}
。由于
人口统计信息
是异步加载的,因此您应该使用安全导航操作符
(?)

请参阅这将对您有所帮助。您可以轻松地执行
{{demographics?.FirstName}}
请查看这将对您有所帮助。您可以轻松地执行
{{{demographics?.FirstName}}
{
    "Demographics" : { 
            "ProfileId":1,
            "FirstName":"vinit",
            "LastName":"mapari",
            "Birthdate":"21/12/1996",
            "MartialStatus":["married","unmarried"],
            "Gender":["Male","Female"],
            "Height":"5ft2inch",
            "ValidIdTypes":["Pancard","adharcard","driving license"],
            "ValidIdNumber":"123",
            "Nationality":"Indian",
            "Religion":"Hindu",
            "BloodGroup":"A",
            "NearestRailwayStation":"byculla"

        }
}
<ul>
        <li><b>Demographics</b></li>
        <li>FirstName:{{ demographics.FirstName }}</li>
</ul>