我有这样的结构
0:{id_schedule: 1}
1:{group_id: 1, last_name: "ame", name: "name", surname: "name"}
...
这是我的服务:
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();
});
}
而且我需要在变量中存储第一个元素 - “id_schedule”,该怎么做?我的组件:
constructor(private http:RequestService,private router: Router) {
this.http.lessonInfoList.subscribe(result => {
this.lessonInfoList = result;
我觉得我能做的就像这样,但我错了:
this.id_schedule = result[0].id_schedule;
在你的情况下,一切都是正确的,但是......这是行为主题,所以你的第一个值将为null,接下来是你想得到的所有值。
您必须添加if检查,如下所示:
constructor(private http:RequestService,private router: Router) {
this.http.lessonInfoList.subscribe(result => {
if(result) {
this.lessonInfoList = result;
this.id_schedule = result[0].id_schedule;
}