angular 2 中的完成参数无效
finish parameter in angular 2 didn't work
从 API 得到结果后,我使用 subscribe 来执行代码行。这是我的代码
this.attRecService.getAgendaData(moment(this.viewDate).format('YYYY-MM')).subscribe(
resp => {
this.agendaData = resp;
for (let item of resp) {
if (item.schedule) {
for (let sched of item.schedule) {
this.events.push({
'start': new Date(moment(item.date).format('YYYY-MM-DD')),
'title': sched.title
});
}
}
}
},
() => {
console.log('display');
this.displayClockDetail();
}
);
但在我尝试控制台记录单词 display 的部分,它似乎不会进入该参数.怎么了?
这个函数
() => {
console.log('display');
this.displayClockDetail();
}
只有在请求失败(发生一些错误)并且之前没有正确处理时才会工作。
如果你想让一个函数在 observable 完成时工作,你可以将第三个参数传递给 subscribe
函数。
.subscibe(() => { ok }, () => { error }, () => { completed })
尝试这样的事情,在控制台中检查显示的消息后
this.attRecService.getAgendaData(moment(this.viewDate).format('YYYY-MM'))
.subscribe((res: any) => {
if(res) {
for (let item of resp) {
if (item.schedule) {
for (let sched of item.schedule) {
this.events.push({
'start': new Date(moment(item.date).format('YYYY-MM-DD')),
'title': sched.title
});
}
}
}
console.log('display');
}
}, (error?: any) => {
console.log('error');
});
从 API 得到结果后,我使用 subscribe 来执行代码行。这是我的代码
this.attRecService.getAgendaData(moment(this.viewDate).format('YYYY-MM')).subscribe(
resp => {
this.agendaData = resp;
for (let item of resp) {
if (item.schedule) {
for (let sched of item.schedule) {
this.events.push({
'start': new Date(moment(item.date).format('YYYY-MM-DD')),
'title': sched.title
});
}
}
}
},
() => {
console.log('display');
this.displayClockDetail();
}
);
但在我尝试控制台记录单词 display 的部分,它似乎不会进入该参数.怎么了?
这个函数
() => {
console.log('display');
this.displayClockDetail();
}
只有在请求失败(发生一些错误)并且之前没有正确处理时才会工作。
如果你想让一个函数在 observable 完成时工作,你可以将第三个参数传递给 subscribe
函数。
.subscibe(() => { ok }, () => { error }, () => { completed })
尝试这样的事情,在控制台中检查显示的消息后
this.attRecService.getAgendaData(moment(this.viewDate).format('YYYY-MM'))
.subscribe((res: any) => {
if(res) {
for (let item of resp) {
if (item.schedule) {
for (let sched of item.schedule) {
this.events.push({
'start': new Date(moment(item.date).format('YYYY-MM-DD')),
'title': sched.title
});
}
}
}
console.log('display');
}
}, (error?: any) => {
console.log('error');
});