Angular http可观测服务中具有延迟的角度4格式阵列
我需要显示和编辑http服务返回的条目列表,这些条目的格式为Angular 4。因为我有不确定的名单,我需要正式的安排。但我的问题是,当我修补阵列时,返回可观察数据的http服务没有提供数据。因此,当我循环遍历返回的数据集时,它抛出错误“无法读取未定义的属性x”。我是一个新的可观察的人,所以我无法找到解决方案。你能帮忙吗 服务:Angular http可观测服务中具有延迟的角度4格式阵列,angular,rxjs,observable,Angular,Rxjs,Observable,我需要显示和编辑http服务返回的条目列表,这些条目的格式为Angular 4。因为我有不确定的名单,我需要正式的安排。但我的问题是,当我修补阵列时,返回可观察数据的http服务没有提供数据。因此,当我循环遍历返回的数据集时,它抛出错误“无法读取未定义的属性x”。我是一个新的可观察的人,所以我无法找到解决方案。你能帮忙吗 服务: getCountries() : Observable<Country[]>{ console.log('In getCountries');
getCountries() : Observable<Country[]>{
console.log('In getCountries');
return this.http
.get(this.apiUrl)
.map(response => <Country[]>response.json());
}
getCountries():可观察{
console.log('In getCountries');
返回此文件。http
.get(this.apiUrl)
.map(response=>response.json());
}
组成部分:
ngOnInit(){
this.getCountries();
this.initForm();
this.patchForm();
}
getCountries(){
this.countryService.getCountries()
.subscribe( response => {
this.countries = response;
console.log('response');
console.log(response);
},
error => {
console.log('error');
console.log(error);
});
console.log('this.countries');
console.log(this.countries);
}
patchForm(){
const control = <FormArray>this.countryForm.controls.countries;
console.log('in patchForm()');
console.log(this.countries);
this.countries.forEach(element => {
control.push(this.formBuilder.group({
id: element.id,
name: element.name,
deleted: element.deleted
}));
});
}
ngOnInit(){
这个。getCountries();
this.initForm();
这个。patchForm();
}
getCountries(){
this.countryService.getCountries()
.订阅(响应=>{
这个国家=回应;
console.log('response');
控制台日志(响应);
},
错误=>{
console.log('error');
console.log(错误);
});
console.log('this.countries');
console.log(this.countries);
}
patchForm(){
const control=this.countryForm.controls.countries;
log('in patchForm()');
console.log(this.countries);
this.countries.forEach(元素=>{
控件。推送(this.formBuilder.group({
id:element.id,
名称:element.name,
已删除:element.deleted
}));
});
}
来自getCountries()的响应将显示在最后的浏览器控制台中。似乎我缺少了一些异步概念。您需要将
补丁形式
移动到成功
回调的可观察
ngOnInit(){
this.initForm();
this.getCountries();
}
getCountries(){
this.countryService.getCountries()
.subscribe( response => {
this.countries = response;
this.patchForm();
console.log('response');
console.log(response);
},
error => {
console.log('error');
console.log(error);
});
console.log('this.countries');
console.log(this.countries);
}
谢谢你,蒂姆。这对我有用。现在我了解了订阅一个可观测对象是如何工作的。