[英]How to display JSON objects in Angular2 views
New to Angular and I have the JSON available to me in the console. Angular的新手,我在控制台中可以使用JSON。 So the backend/REST call is functioning correctly.
所以后端/ REST调用正常运行。 However I'm struggling to understand how to show the JSON in the view for the component.
但是我很难理解如何在组件的视图中显示JSON。
import { Component } from 'angular2/core';
import { TradeshowComponent } from './tradeshow/tradeshow.component';
@Component({
selector: 'app-container'
})
export class AppComponent {
constructor() { }
}
import { Component, View } from 'angular2/core';
import { CORE_DIRECTIVES, NgIf, NgFor } from 'angular2/common';
import { DataService } from '../shared/services/data.service';
import { DashboardLayoutComponent } from '../dashboard_layout/dashboard_layout.component';
import { HTTP_PROVIDERS } from 'angular2/http';
@Component({
selector: 'tradeshow',
providers: [DataService, HTTP_PROVIDERS]
})
@View({
templateUrl: 'src/app/tradeshow/tradeshow.component.html',
directives: [DashboardLayoutComponent, NgIf, NgFor]
})
export class TradeshowComponent {
constructor(private _dataService: DataService) { this.getTradeShows() }
getTradeShows() {
this._dataService.getTradeShows()
.subscribe(
tradeshows => this.tradeShows = tradeshows
error => console.error('Error: ' + err)
);
}
}
And the HTML I'm using is: 我正在使用的HTML是:
<div *ngFor="#tradeshows of tradeshows">{{ tradeshows.name }}</div>
And my service looks like this: 我的服务看起来像这样:
import { Injectable } from 'angular2/core';
import { Http, Response } from 'angular2/http';
import {Observable} from 'rxjs/Observable';
import {Config} from '../../config/config';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class DataService {
// API path
baseUrl: string = '/api';
authUrl: string;
apiUrl: string;
registerUrl: string;
constructor(private _http: Http, private _config: Config) {
this.apiUrl = this._config.get('apiUrl') + this.baseUrl;
this.authUrl = this._config.get('apiUrl') + '/auth';
this.registerUrl = this._config.get('apiUrl') + '/register';
}
getTradeShows() {
return this._http.get(this.getApiUrl('/tradeshow/list'))
.map((res: Response) => res.json())
.catch(this.handleError);
}
}
This looks like a bug 这看起来像一个bug
getTradeShows() {
this._dataService.getTradeShows()
.subscribe(
tradeshows => this.getTradeShows() = tradeshows,
error => console.error('Error: ' + err)
);
}
it should be 它应该是
getTradeShows() {
this._dataService.getTradeShows()
.subscribe(
tradeshows => this.tradeshows = tradeshows
error => console.error('Error: ' + err)
);
}
You can remove NgIf, NgFor
from 你可以删除
NgIf, NgFor
directives: [DashboardLayoutComponent, NgIf, NgFor]
these are now globally available. 这些现在全球可用。
Change 更改
<div *ngFor="#tradeshows of tradeshows">{{ tradeshows.name }}</div>
to 至
<div *ngFor="#tradeshow of tradeshows">{{ tradeshow.name }}</div>
@Child()
was removed a while ago @Child()
前一段时间被删除了
})
@View({
should be replaced by ,
应该被替换
,
I made an error in this line 我在这一行犯了一个错误
tradeshows => this.tradeShows = tradeshows
should be (lowercase S) 应该是(小写S)
tradeshows => this.tradeshows = tradeshows
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.