2

I have this code which reads some json data:

import {Component} from 'angular2/core';
import {Http, Response} from 'angular2/http';

@Component({
    selector: 'my-app',
    template: `
      <h2>Basic Request</h2>
      <button type="button" (click)="makeRequest()">Make Request</button>
      <div *ngIf="loading">loading...</div>
      <pre>{{data | json}}</pre>

    `

})
export class AppComponent {

    data: Object;
    loading: boolean;

    constructor(public http: Http) {
    }
    makeRequest(): void {
        this.loading = true;
        this.http.request('http://jsonplaceholder.typicode.com/photos/1')
            .subscribe((res: Response) => {
                this.data = res.json();
                this.loading = false;
            }); }

}

This is the returned json:

{
  "albumId": 1,
  "id": 1,
  "title": "accusamus beatae ad facilis cum similique qui sunt",
  "url": "http://placehold.it/600/92c952",
  "thumbnailUrl": "http://placehold.it/150/30ac17"
}

{{data | json}} is returning all the data.

I wanted to just get the title for example. So I tried this:

{{data.title | json}} but this doesn't work.

What is the right way to display just the title?

4 Answers 4

7

Use elvis operator like this

<pre>{{data?.title}}</pre>

The Elvis operator (?) means that the data field is optional and if undefined, the rest of the expression should be ignored.

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

Comments

3

Add a map and switch to get

   this.data = {}; //initialize to empty object
   this.http.get('http://jsonplaceholder.typicode.com/photos/1')
            .map((res: Response) => res.json())
            .subscribe(res => {
                this.data = res;
                this.loading = false;
            });

Then in the template use it like this {{data.title}}

Here are a few more http samples: http://www.syntaxsuccess.com/viewarticle/angular-2.0-and-http

2 Comments

This is the data: { "albumId": 1, "id": 1, "title": "accusamus beatae ad facilis cum similique qui sunt", "url": "placehold.it/600/92c952", "thumbnailUrl": "placehold.it/150/30ac17" }
Need a map and a get call
0

Data is a JSON Object, so if you use data then use the | json pipe. If you want to use a value which is not an Object, then just use the value directly

{{data.title}}

2 Comments

That doesn't return anything ...Editor says: Unresolved variable title
I have the same problem. Just writing the object in the html code prints "[object Object] " in the view. Trying to get any property results in "EXCEPTION: TypeError: l_fav0 is undefined in [null]", where "fav" is the name of my object. I dont know what I am missing but its the same problem with as Satch3000. The whole net just gives the same advices which do no help and no examples except too simple ones can be found.
0

you can try this

 this._http.request('http://jsonplaceholder.typicode.com/photos/1' )
            .map((res:Response) => res.json()) //add this line
            .subscribe(
                data => this.myData = data,
                this.loading = false;
                error =>this.logError(error),
            );

you can now do {{myData.title}}

For more than one object use : ngFor like this

<li *ngFor="let item of myData">
        {{item.title}}
    </li> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.