Javascript 如何在RXJS子描述中运行多个条件
我正在从一个模式中获取数据,我需要检查结果,并根据许多条件确定触发哪个外部功能 我已经验证了被传递回的数据格式正确,并且应该触发一系列else-if,但是看起来它将跳过所有条件而不执行任何操作 实现这种逻辑最有效的方法是什么 我的代码:Javascript 如何在RXJS子描述中运行多个条件,javascript,angular,rxjs,Javascript,Angular,Rxjs,我正在从一个模式中获取数据,我需要检查结果,并根据许多条件确定触发哪个外部功能 我已经验证了被传递回的数据格式正确,并且应该触发一系列else-if,但是看起来它将跳过所有条件而不执行任何操作 实现这种逻辑最有效的方法是什么 我的代码: dialogRef.afterClosed().subscribe(result => { console.log('RESULT: ', result.delete); if (result.isNew && !re
dialogRef.afterClosed().subscribe(result => {
console.log('RESULT: ', result.delete);
if (result.isNew && !result.cancel) {
console.log('new story!');
this.boardService.updateTasks(this.job.id, [
...this.job.tasks,
result.task
]);
this.closeStories('update', this.job)
}
// means a delete has occured
else if (result.delete) {
this.closeStories('deleted triggered...')
// last story has been deleted, prompt newStory
if (this.stories.length < 2 && result === {}) {
console.log('Last story was deleted!');
this.closeStories('delete')
}
// a delete has happened
else if (this.stories.length > 1 && result === {}) {
console.log('A story was deleted!');
this.closeStories('deletedLast')
}
}
// means an update has occured
else if (result.id && !result.isNew) {
console.log('A story was Updated!');
const update = this.stories;
update.splice(result.index, 1, result.task);
this.boardService.updateTasks(this.job.id, update);
this.closeStories('update', this.job)
}
else if (result === undefined) {
return 0
}
// this.closedStoryListEmitter.emit(this.job);
console.log('hit close list event emitter!!');
});
dialogRef.afterClosed().subscribe(结果=>{
console.log('RESULT:',RESULT.delete);
if(result.isNew&&!result.cancel){
console.log('newstory!');
this.boardService.updateTasks(this.job.id[
…这个工作任务,
结果任务
]);
this.closeStories('update',this.job)
}
//表示已发生删除
else if(result.delete){
此.closeStories('已删除已触发…'))
//最后一篇报道已被删除,即时新闻报道
if(this.stories.length<2&&result==={}){
log('Last story已删除!');
this.closeStories('delete'))
}
//已发生删除
else if(this.stories.length>1&&result==={}){
console.log('一篇报道被删除!');
this.closeStories('deletedLast'))
}
}
//表示已发生更新
else if(result.id&!result.isNew){
console.log('更新了一个故事!');
const update=this.stories;
更新.splice(result.index,1,result.task);
this.boardService.updateTasks(this.job.id,update);
this.closeStories('update',this.job)
}
else if(结果===未定义){
返回0
}
//this.closedStoryListEmitter.emit(this.job);
log('hit-close-list-event-emitter!!');
});
我认为,如果您希望触发if语句,只需执行多个if语句块而不使用else-if块,或者至少我会这样做
...
if (result.isNew && !result.cancel) {
console.log('new story!');
this.boardService.updateTasks(this.job.id, [
...this.job.tasks,
result.task
]);
this.closeStories('update', this.job)
}
if (result.delete) {
this.closeStories('deleted triggered...')
// last story has been deleted, prompt newStory
if (this.stories.length < 2 && result === {}) {
console.log('Last story was deleted!');
this.closeStories('delete')
}
// a delete has happened
else if (this.stories.length > 1 && result === {}) {
console.log('A story was deleted!');
this.closeStories('deletedLast')
}
}
if (result.id && !result.isNew) {
console.log('A story was Updated!');
const update = this.stories;
update.splice(result.index, 1, result.task);
this.boardService.updateTasks(this.job.id, update);
this.closeStories('update', this.job)
}
if (result === undefined) {
return 0
}
...
。。。
if(result.isNew&&!result.cancel){
console.log('newstory!');
this.boardService.updateTasks(this.job.id[
…这个工作任务,
结果任务
]);
this.closeStories('update',this.job)
}
if(result.delete){
此.closeStories('已删除已触发…'))
//最后一篇报道已被删除,即时新闻报道
if(this.stories.length<2&&result==={}){
log('Last story已删除!');
this.closeStories('delete'))
}
//已发生删除
else if(this.stories.length>1&&result==={}){
console.log('一篇报道被删除!');
this.closeStories('deletedLast'))
}
}
if(result.id&!result.isNew){
console.log('更新了一个故事!');
const update=this.stories;
更新.splice(result.index,1,result.task);
this.boardService.updateTasks(this.job.id,update);
this.closeStories('update',this.job)
}
如果(结果===未定义){
返回0
}
...
我认为,如果您希望触发if语句,只需执行多个if语句块而不使用else-if块,或者至少我会这样做
...
if (result.isNew && !result.cancel) {
console.log('new story!');
this.boardService.updateTasks(this.job.id, [
...this.job.tasks,
result.task
]);
this.closeStories('update', this.job)
}
if (result.delete) {
this.closeStories('deleted triggered...')
// last story has been deleted, prompt newStory
if (this.stories.length < 2 && result === {}) {
console.log('Last story was deleted!');
this.closeStories('delete')
}
// a delete has happened
else if (this.stories.length > 1 && result === {}) {
console.log('A story was deleted!');
this.closeStories('deletedLast')
}
}
if (result.id && !result.isNew) {
console.log('A story was Updated!');
const update = this.stories;
update.splice(result.index, 1, result.task);
this.boardService.updateTasks(this.job.id, update);
this.closeStories('update', this.job)
}
if (result === undefined) {
return 0
}
...
。。。
if(result.isNew&&!result.cancel){
console.log('newstory!');
this.boardService.updateTasks(this.job.id[
…这个工作任务,
结果任务
]);
this.closeStories('update',this.job)
}
if(result.delete){
此.closeStories('已删除已触发…'))
//最后一篇报道已被删除,即时新闻报道
if(this.stories.length<2&&result==={}){
log('Last story已删除!');
this.closeStories('delete'))
}
//已发生删除
else if(this.stories.length>1&&result==={}){
console.log('一篇报道被删除!');
this.closeStories('deletedLast'))
}
}
if(result.id&!result.isNew){
console.log('更新了一个故事!');
const update=this.stories;
更新.splice(result.index,1,result.task);
this.boardService.updateTasks(this.job.id,update);
this.closeStories('update',this.job)
}
如果(结果===未定义){
返回0
}
...
im根据结果运行条件。您的示例就是我已经准备好的,但是只有当响应一切正常时,您的代码才会执行。若请求中有错误,例如,若服务器返回任何404,那个么代码将不会运行。它将查找错误回调。是的,这是正确的,我在前面的问题中说过,我从resultOk接收到预期值,对此我不确定,语言障碍等等:)无论如何,您是否尝试设置断点来检查它停止执行的位置?因为如果你得到了正确的结果,我唯一能想到的就是你的情况有问题。你能发布结果值吗?谢谢你的帮助!看来问题出在其他人身上。如果我使用普通的“if”语句,它将在结果上运行条件。您的示例就是我已经准备好的,但是只有当响应一切正常时,您的代码才会执行。若请求中有错误,例如,若服务器返回任何404,那个么代码将不会运行。它将查找错误回调。是的,这是正确的,我在前面的问题中说过,我从resultOk接收到预期值,对此我不确定,语言障碍等等:)无论如何,您是否尝试设置断点来检查它停止执行的位置?因为如果你得到了正确的结果,我唯一能想到的就是你的情况有问题。你能发布结果值吗?谢谢你的帮助!看来问题出在其他人身上。如果我使用简单的“if”语句,它就可以工作了。你有没有试过只使用一系列if语句?@EJSulit yes,这似乎只适用于ifsgreat!我刚看到你的评论,我以为你不在,所以我贴了一个答案,哈哈,祝你好运!你试过只做一系列if语句吗?@EJSulit是的,这似乎只适用于ifsgreat!我刚看到你的评论,我以为你不在,所以我贴了一个答案,哈哈,祝你好运!