たぶん、あなたはこのようなものが必要です。そうしない場合は
<div *ngFor="let user of users | async">
<span>Maybe print username: {{user.name}}</span>
</div>
:あなたのコンポーネントで
export class UserService {
getUsers() {
return fetch('http://localhost:8000/api/users')
.then(function(response) {
console.log(response);
return response.json();
});
}
}
:
export class UserComponent implements OnInit {
users: any;
constructor(private userService: UserService) {}
ngOnInit() {
this.users = this.userService.getUsers();
}
}
、テンプレートでasync
パイプを使用してasync
パイプを使用します。
コンポーネントのコード:
export class UserComponent implements OnInit {
users: any;
constructor(private userService: UserService) {}
ngOnInit() {
this.userService.getUsers().then(data => {
// do something with response data
console.log(data);
this.users = this.someMethodToTransformResponse(data);
});
}
someMethodToTransformResponse(data) {
// return modification your data
}
}
とテンプレート:
<div *ngFor="let user of users">
<span>Maybe print username: {{user.name}}</span>
</div>
あなたには「返品」がありません。それは 'return response.json()'でなければなりません – Phil
また、あなたは 'users'に約束を割り当てています。これは、 'this.userService.getUsers()。(users => this.users = users)' – Phil
を試したくないかもしれません。@Phil多分、彼はテンプレートで 'async'パイプを使用したいと思っています:) –