I have a structure like this
0:{id_schedule: 1}
1:{group_id: 1, last_name: "ame", name: "name", surname: "name"}
...
It's my service:
public lessonInfoList : BehaviorSubject<any> = new BehaviorSubject<any>(null);
getLessonInfo(model:any): Observable<any>{
return this.http.post("http://127.0.0.1:5000/lessoninfo",JSON.stringify(model))
.map((response:Response)=>{
this.lessonInfoList.next(response.json());
return response.json();
});
}
And I need to store first element - "id_schedule" in variable, how to do it? My component:
constructor(private http:RequestService,private router: Router) {
this.http.lessonInfoList.subscribe(result => {
this.lessonInfoList = result;
I thought what I can do smth like this, but I was wrong:
this.id_schedule = result[0].id_schedule;
In your case all is correct, but ... It is Behavior subject, so your first value will be null, the next all what you want to get.
You have to add an if check just like this:
constructor(private http:RequestService,private router: Router) {
this.http.lessonInfoList.subscribe(result => {
if(result) {
this.lessonInfoList = result;
this.id_schedule = result[0].id_schedule;
}