today I use angular 2 to write a http request.I got the data in service,but I got undefinded
in component
.I don't know how to solve it,please give me some help.
service
export class AdminStoreService {
data:Object;
constructor(http:Http) {
this.http = http;
}
findAll() {
return this.http
.get('/data/admin.json')
.map((res:Response) => {
console.log(data); //can get the json data
this.data = res.json();
});
}
}
console.log(data)
component
export class AdminComponent {
admins:Object;
constructor(adminStore:AdminStoreService) {
this._adminStore = adminStore;
this._adminStore.findAll().subscribe(data => this.admins = data);
console.log(this.admins); // undefined
}
}
template can't get the data
<section *ngIf="admins">
<tr *ngFor="let admin of admins" class="animated fadeInRight">
<td>{{admin.username}}</td>
<td class="alert">{{admin.authLevel}}</td>
<td>{{admin.operator}}</td>
<td>{{admin.created | dateFormat:'YYYY-MM-DD HH:mm:ss'}}</td>
<td>{{admin.updated | dateFormat:'YYYY-MM-DD HH:mm:ss'}}</td>
</tr>
</section>