简体   繁体   English

如何处理 angular 中的异步操作?

[英]How to handle async operation in angular?

I am trying to initiate a modal component on ngOnInit() by passing fragments in route link我正在尝试通过在路由链接中传递片段来启动 ngOnInit() 上的模态组件

  ngOnInit() {
    this.getCandidateDetails()

    this.route.fragment.subscribe(qp => {
      if ('openjobeditmodal' == qp) {
        this.editJobRole();
      }
    })
    
  }

now I am able to open a modal component on page initiation but the issue is that component takes this.getCandidateDetails() response as Input,and this.getCandidateDetails() is getting called after modal initiate,how can I ensure thatI only subscribe to modal only after this.getCandidateDetails() gets called现在我可以在页面启动时打开模态组件,但问题是组件将 this.getCandidateDetails() 响应作为输入,并且this.getCandidateDetails()在模态启动后被调用,我如何确保我只订阅模态只有在调用this.getCandidateDetails()之后

Make this.getCandidateDetails() return a Promise, then await the Promise completion before subscribing the observable:使 this.getCandidateDetails this.getCandidateDetails()返回 Promise,然后在订阅可观察对象之前等待 Promise 完成:

async ngOnInit() {
  await this.getCandidateDetails()

  this.route.fragment.subscribe(qp => {
    if ('openjobeditmodal' == qp) {
      this.editJobRole();
    }
  })
}

Share the code of your getCandidateDetails() if you want help at making it return a promise.如果您需要帮助使其返回 promise,请分享您的getCandidateDetails()代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM