【发布时间】:2018-03-03 15:51:34
【问题描述】:
我是 Angular2 的新手,我想加载这个 Json 数据并显示在页面中,我不知道该怎么做..?从所有来源中我了解到我制作了一个代码并将其附在下面,但是由于一些错误它没有运行,任何人都可以帮助我修复或编写一个新代码,以便我可以从中学习..
提前感谢您的帮助。
我的代码文件 - student.json
[
{
"name": "John",
"id_number": "12",
"attendance": "276 days",
"grade": "A"
},
],
这是我的 students.service.ts 代码
import {Injectable} from '@angular/core';
import { Http, Response } from '@angular/http';
@Injectable()
export class StudentsService {
constructor(private http:Http)
{
}
getStudents() {
return this.http.get('./students.json')
.map((response: Response) => {
console.log("mock data" + response.json());
return response.json();
}
}
而且,这是我的 students.component.ts 文件
import {Component} from '@angular/core';
import { Http, Response } from '@angular/http';
import {StudentsService} from './students.service';
import 'rxjs/add/operator/map'
import 'rxjs/Rx';
@Component({
selector: 'students',
templateUrl: 'students.html',
styleUrls: ['./students.scss']
})
export class students {
public students;
constructor( private _studentsService:StudentsService, private http:Http)
{
this.students = this._studentsService.getStudents();
}
ngOnInit() {
this._load();
}
private _load() {
this.students = this._studentsService.getStudents();
}
}
【问题讨论】:
-
只需将其分配给一个变量并在模板中使用像
{{var|json}}这样的var
标签: javascript json angular