제 상사가 Angular2를 프로젝트에 구현하기로 결정했습니다. 나는이 기술에 대해 새롭다. URL에서 JSON을 표시하려고하지만 예상 한 결과가 아닙니다. * ngFor는 데이터를 표시하지 않습니다. Angular2 - json 객체를 표시 할 수 없습니다.
이
코드입니다 :vehicle.service.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class VehicleService {
constructor(private http: Http){ }
getVehicle() {
return this.http.get('https://jsonplaceholder.typicode.com/posts')
.map(res => res.json());
}}
vehicle.component.ts
import { Component } from '@angular/core';
import { VehicleService } from './vehicle.service';
@Component({
moduleId: module.id,
selector: 'vehicle-json',
templateUrl: 'vehicle.html',
providers: [ VehicleService ]
})
export class VehicleComponent {
vehicle: Vehicle[];
constructor(private vehicleService: VehicleService){
this.vehicleService.getVehicle().subscribe(vehicle => {
/*console.log(vehicle);*/this.vehicle = vehicle;
});
}
}
interface Vehicle {
id: number;
title: string;
body: string;
}
vehicle.html
<div *ngFor="let vehicle of vehicles">
<h3>{{vehicle.title}}</h3>
<p>{{vehicle.body}}</p>
</div>
test 1-2-3
출력은 "test 1-2-3"입니다. jsons가 콘솔 내부에 표시되는지 확인했습니다 : console.log(vehicle);
- 작동 했으므로 Array of Objects를 반환합니다.
내가 뭘 잘못하고 있니? 어떤 아이디어를 내 코드를 수정하는 방법?
안녕하세요. 작동합니다. 나는 vehicle : Vehicle []을'vehicles : Vehicle [];로 변경하고 제안을 적용했다. 나는 그처럼 작은 실수를 한 것이 부끄럽다. 고맙습니다. – Haseoh
당신은 환영합니다, 때때로 당신은 신선한 눈이 필요합니다! :) – Alex