[英]Angular2: Async data loading from server
I want to load data from server in my Angular app . 我想在Angular应用中从服务器加载数据。 But data request is
async
, and I can't get data properly. 但是数据请求是
async
,我无法正确获取数据。 First I pass url parameter. 首先,我传递url参数。 Based on parameter, I load data from
ngOnInit()
, but data is not loading properly. 根据参数,我从
ngOnInit()
加载数据,但是数据加载不正确。 I've googling hour, but no solution. 我已经搜索了一个小时,但没有解决方案。 My code is given below:
我的代码如下:
Service 服务
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import {Author} from './author';
@Injectable()
export class AuthorService {
private headers = new Headers({
'Content-Type': 'application/json'
});
private authorUrl = 'api/author';
constructor(private http:Http) { }
getAuthor(authorId : String):Promise<Author> {
return this.http.get(this.authorUrl + '/get/'+ authorId )
.toPromise()
.then(res => res.json().data as Author)
.catch(this.handleError)
}
}
Component 零件
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AuthorService } from '../author/author.service';
import { Author } from '../author/author';
@Component({
selector: 'book-list',
templateUrl: './book.component.html'
})
export class BookComponent implements OnInit {
constructor(private route: ActivatedRoute, private authorService: AuthorService) { }
fetchAuthor( authorId ): Promise<Author> {
return this.authorService.getAuthor( authorId )
}
ngOnInit() : void {
this.fetchAuthor( this.id ).then( (author) => {
this.author = author; // here value is avaliab
} );
// here value is undefined
console.log('author data : ', this.author);
/*
// I also have tried this
this.authorService
.getAuthor( this.id )
.then(author => {
this.author = author;
console.log(this.author); // here value available
});
console.log(this.author); // here value is not available
*/
};
}
HTML HTML
<div class="form-group">
<label for="exampleInputEmail1">Initial</label>
<!-- this code causes error -->
<p> {{author.initial}} </p>
</div>
Any suggestion? 有什么建议吗? Thanks in Advance.
提前致谢。
Try this safe operator/elvis operator (?)
: 试试这个
safe operator/elvis operator (?)
:
<div class="form-group">
<label for="exampleInputEmail1">Initial</label>
<!-- this code causes error -->
<p> {{author?.initial}} </p>
</div>
by using elevis operator (?)
[also called safe navigation operator
] it will prevent angular to throw error , beneficial in case where data is coming asynchronously like in your case 通过使用elevis运算符
(?)
[也称为safe navigation operator
],它将防止角度抛出错误,这在数据异步传输的情况下(如您的情况)很有用
Here is detailed information about this 这是有关此的详细信息
https://angular.io/docs/ts/latest/guide/template-syntax.html#!#safe-navigation-operator
https://angular.io/docs/ts/latest/guide/template-syntax.html#!#safe-navigation-operator
You can use the safe operator
to check if the data is available then bind it, it will prevent angular to throw error , 您可以使用
safe operator
检查数据是否可用,然后将其绑定,这将防止角度抛出错误,
<label for="exampleInputEmail1">Initial</label>
<p> {{author?.initial}} </p>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.