gpt4 book ai didi

json - angular2 从服务打印 json

转载 作者:太空狗 更新时间:2023-10-29 17:11:19 26 4
gpt4 key购买 nike

我不知道如何从模板中打印的 JSON 获取数据

如果我在模板中使用:模板:人物:{{people}} 或模板:人物:{{articles}}
始终在浏览器中获取:

people: [object Object]

如果我在模板中使用:模板:人物:{{people.totalrecords}} 或模板:人物:{{articlestotalrecords}}

在获取空白值时:人们:

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>

最佳答案

如果你想看到json,你只需要这样做:

people: {{people | json}} 

关于json - angular2 从服务打印 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35243119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com