[英]How to perform http call in angular2
I am trying to get the details for edit purpose but i am not sure how to do it with observables can some one help me the issue is with edit function. 我正在尝试获取用于编辑目的的详细信息,但我不确定如何使用可观察对象进行操作是否可以帮助我解决编辑功能方面的问题。
@Component({
selector: 'Search',
templateUrl: './components/search/search.html',
directives: [REACTIVE_FORM_DIRECTIVES],
providers : [GetSocietyList],
})
export class Search {
property:any = 'Add';
data: string;
form:any;
prop: string = 'connenct';
details: IStudent1[];
details1: IStudent2[];
constructor(fbld: FormBuilder,public http: Http,private _profileservice:GetSocietyList) {
this.details = [];
this.http = http;
this._profileservice.getSocietyList()
.subscribe(details => this.details = details);
console.log(this.details);
this.form = fbld.group({
name: [''],
age: ['', Validators.required],
class: ['', Validators.required],
grade: ['', Validators.required]
});
}
edit(id): any {
//console.log(id);
this.property = 'update';
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded')
this.http.get('http://localhost/a2server/index.php/profile/editprofiledb/' + id, { headers: headers })
.subscribe(response => {
if (response.json().error_code == 0) {
this.details = <IStudent2[]>response.json().data;
} else {
this.details = <IStudent2[]>response.json().data;
}
} )}
I am trying to get the details for edit purpose but i am not sure how to do it with observables can some one help me the issue is with edit function. 我正在尝试获取用于编辑目的的详细信息,但我不确定如何使用可观察对象进行操作是否可以帮助我解决编辑功能方面的问题。
this._profileservice.getSocietyList()
.subscribe(details => this.details = details);
console.log(this.details);
this will always log undefined
because you're logging right after subscribing to the observable. 这将始终记录
undefined
日志,因为您是在订阅观察对象后立即进行记录。 You need to wait for the request to finish to see the data: 您需要等待请求完成才能查看数据:
this._profileservice.getSocietyList()
.subscribe(details => {
this.details = details;
console.log(this.details);
});
So your data fetching most likely works, you're just trying to log it too eagerly. 因此,您最有可能获取数据的方法是,您只是试图太热切地记录下来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.