8

I don't know how to get the data from JSON printed in my template

if i use in template: template: people: {{people}} or template: people: {{articles}}
always get in the browser:

people: [object Object]

if i use in template: template: people: {{people.totalrecords}} or template: people: {{articlestotalrecords}}

in get blank value: people:

import {bootstrap} from 'angular2/platform/browser';
import {Component, enableProdMode, Injectable, OnInit} from 'angular2/core';
import {Http, HTTP_PROVIDERS, URLSearchParams} from 'angular2/http';
import 'rxjs/add/operator/map';
import {enableProdMode} from 'angular2/core';

@Injectable()
class ArticleApi {
  constructor(private http: Http) {}
  getData: string;

  seachArticle(query) {
    const endpoint = 'http://xdemocrm.com/webservicecrm.php';
    const searchParams = new URLSearchParams()
    searchParams.set('object', 'account');
    searchParams.set('action', 'list');   
    return this.http
      .get(endpoint, {search: searchParams})
      .map(res => res.json())
  }

  postExample(someData) {
    return this.http
      .post('https://yourEndpoint', JSON.stringify(someData))
      .map(res => res.json());
  }
}

@Component({
  selector: 'app',
  template: `people: {{people}}`, 
  providers: [HTTP_PROVIDERS, ArticleApi],
})
class App implements OnInit {
  public people;
  constructor(private articleApi: ArticleApi) { }
  public articles: Array<any> = [];

  ngOnInit() {
    this.articles = this.articleApi.seachArticle('obama')
        .subscribe (data => this.people = data) 
  }
}

bootstrap(App)
  .catch(err => console.error(err));
<!DOCTYPE html>
<html>
<head>
  <title>angular2 http exmaple</title>
  <link rel="stylesheet" href="style.css">
  <script src="https://code.angularjs.org/tools/system.js"></script>
  <script src="https://code.angularjs.org/tools/typescript.js"></script>
  <script src="config.js"></script>
  <script src="https://code.angularjs.org/2.0.0-beta.1/angular2-polyfills.js"></script>
  <script src="https://code.angularjs.org/2.0.0-beta.1/Rx.js"></script>
  <script src="https://code.angularjs.org/2.0.0-beta.1/angular2.js"></script>
  <script src="https://code.angularjs.org/2.0.0-beta.1/http.js"></script>
  <script>
    System.import('app')
      .catch(console.error.bind(console));
  </script>
</head>
<body>
  <app>loading...</app>
</body>
</html>

3 Answers 3

27

If you want to see json, you just need to do this:

people: {{people | json}} 
Sign up to request clarification or add additional context in comments.

Comments

9

I guess you need

{{people?.totalrecords}}

to make it work.

You fetch the data before Angular renders the view, but the response probably arrives afterwards.

With ?. angular doesn't evaluate .totalrecords while people is null.

2 Comments

thanks! so I must use for security always ? in my templates? what about when i have just not an value , can i use {{?totalrecords}} , thanks!
When people is undefined or null people.totalrecords throws because null doesn't have a totalrecords property. You only need ?. when the part before it can become undefined or null. If you assign an initial value to people (for example in the constructor) you don't need .?. Otherwise people is undefined until the value from the server arrives. people?.totalrecords is a short form of people ? people.totalrecords : null
0

If you want something more elegant you can use the HTML <pre> Tag:

<pre> {{people | json}} </pre>

Comments

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.