我有这样的结构
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;
答案 0 :(得分:0)
在你的情况下,一切都是正确的,但...... 它是行为主题,因此您的第一个值将为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;
}