2

I want to show the street of a person. But it didn't work with the code that I have. I think I do it wrong. (to get the name of a person works perfectly) json file:

{
  "id":1,
  "name": "someName",
  "address":{
      "street":"someStreet",
      "number":"10"
  }
}

service get json:

 public getPerson(id:string):Observable<Person> {
               return this.http.get(PersonService.PATH + id + '.json')
                  .map((res:Response) => res.json());
        }

typescript file PersonDetail:

@Component({
    selector: "person",
    template: `
        <h3>Detail of {{person.name}}</h3> --> this works
        <p>{{person.address.street}}</p> --> this not
                  `
})
export class PersonDetail {
    private person:Person = Person.createEmptyPerson();
    private router:Router;

    constructor(personService:PersonService, params:RouteParams, router:Router) {
        personService.getItem(params.get("id")).subscribe((person:Person) => {
            this.person =person;
        });
        this.router = router;
    }
}

Person:

import {Address} from "./address";
export class Person {
    id:number;
    name:string;
   adress:Address;


    constructor(id:number, name:string, adress:Address) {
        this.id = id;
        this.name = name;
        this.adress = adress;
    }


    public static createEmptyPerson():Person{
        return new Person(0, "",null);
    }

}

Addres:

export class Address {
    street:string;
    number:number;


    constructor(street:string, number:number) {
        this.street = street;
        this.number = number;
    }

    public static createEmptyAddress():Address {
        return new Address("",0);
    }

}
9
  • Are you sure person.address is set? Commented Dec 29, 2015 at 23:01
  • how do you mean? Do I have to set person.address in PersonDetail class? Commented Dec 29, 2015 at 23:03
  • What is shown when you change <p>{{person.address.street}}</p> to <p>{{person.address}}</p>? Does this output anything? Commented Dec 29, 2015 at 23:04
  • nothing, the div is empty Commented Dec 29, 2015 at 23:07
  • There might be a problem with creating the person instance. Commented Dec 29, 2015 at 23:08

2 Answers 2

2

I think that it works at the beginning for person.name because you create an empty person within your PersonDetail component. But not for address because the corresponding address is null:

public static createEmptyPerson():Person{
  return new Person(0, '',null);
}

Since HTTP requests are asynchronous, the complete data for the person will be there later.

I see two solutions for your problem:

  • Use an ngIf around your template content:

    template: `
      <div *ngIf="person">
        <h3>Detail of {{person.name}}</h3>
        <p>{{person.address.street}}</p>
      </div>
    `
    
  • As suggested by Eric, use the Elvis operator. But Detail of with nothing after will be displayed.

    template: `
      <h3>Detail of {{person?.name}}</h3>
      <p>{{person?.address?.street}}</p>
    `
    
Sign up to request clarification or add additional context in comments.

3 Comments

I think my service class don't get my address from a person. How can I do that?
Got a problem in this kind, even if the data is present (I checked with console.log), what finally works is the Evlis operator around nested object
@MohamedB : I'm also facing the same issue. Could you resolve the issue ?
1

Don't you have a typo on your Person class?

adress:Address

I believe it should be

address:Address

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.