2

In my Angular RC2 app I make an observable HTTP call that returns the following JSON to me from the API:

{
    "success":true,
    "data":
    {
        "Type": 1
        "Details":{
            "Id":"123",
            "Name":"test",
            "Description":"test"
        }
    }
}

I map the data like this:

this._myService.get(id)
    .subscribe(
        (data) => {
            this.details = data.Details;
            this.type = data.Type;
        },
        (error) => {
            this.setError(error);
        }
     );

How do I access the values inside the "Details" object from here?

I tried:

{{details.Name}}

But that won't work and I can't use ngFor to loop it either.

1 Answer 1

8

You could use the Elvis operator for this:

{{details?.Name}}

As a matter of fact, you load your data asynchronously so details is undefined at the beginning.

Sign up to request clarification or add additional context in comments.

2 Comments

You could also use *ngIf="details" or *ngIf="details.Name" (so you could show something like "loading" for *ngIf="!details")
@bene yes agreed! It depends on the use case ;-)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.