我做了一個組件與simle服務。 我想實現以下行爲 - 當我的服務的http.get請求完成時 - 使用接收到的參數進行另一個獲取請求。這與角2
但是,Groupservice請求的參數this.sections[0].Id
未定義,並且當前沒有section參數。儘管從data.json收到的數據是正確的。
的代碼是(所附進口)
@Component({
template: require('./content.component.html'),
styles: [require('./content.component.css')],
providers: [SectionService, GroupService]
})
export class ContentComponent {
id: string;
sections: Array<Section>;
groups: Array<Group>
selectedSectionTitle: "";
constructor(private router: Router, private activateRoute: ActivatedRoute, private sectionService: SectionService, private groupService: GroupService) {
this.router.events.subscribe(path => {
this.id = activateRoute.snapshot.params['id'];
});
this.id = activateRoute.snapshot.params['id'];
}
ngOnInit() {
this.sectionService.getSections()
.subscribe((data: Response) => {
this.sections = data.json();
}, err => {
console.error("Error occurred while saving setting", err);
},() => {
this.groupService.getGroups(this.sections[0].Id).subscribe((data: Response) => {
this.groups = data.json();
})
});
}
}
export class Group {
isSelectionComponentOpened: boolean;
isItemEditComponentOpened: boolean;
Id: string;
Title: string;
SectionId: string;
Items: Array<string>
}
export class Section {
Id: string;
Title: string;
}
@Injectable()
export class SectionService {
constructor(private http: Http) { }
// Переделать на Observable
getSections() {
return this.http.get('Section/Get');
}
}
我不能找出這個問題的原因。 請指教。
這是角2,我很抱歉。 – Greenmachine