最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular2 - turn 'activatedRoute.params' into promise - Stack Overflow

programmeradmin1浏览0评论

I am trying to turn the observable for params that I get off of the ActivatedRoute into a promise but haven't had any luck. I have been able to turn http requests into promises successfully:

this.riaService.getAdvisorsForState(this.activeState)
    .then(rias => {
        this.riasForState = rias.json();
        console.log(this.riasForState);
    });
// this all works ^

But I have not been able to turn the 'activatedRoute.params' into a promise:

export class InvestmentAdvisorStateComponent implements OnInit {

    constructor(private activatedRoute: ActivatedRoute, private riaService: InvestmentAdvisorService) { }

    getStateFromUrl() {
        this.activatedRoute.params
            .toPromise()
            .then(params => {
                console.log('HERE',params)
            });
    }
// this does not work for me ^

This is what I have right now:

getStateFromUrl() {
    this.activatedRoute.params
        .subscribe((param: any) => {
            this.activeState = param['state'];
        });
}
// works ^

I am hoping to implement this as a promise thought so I can .then off of it. Any help on how to do this is greatly appreciated.

I am trying to turn the observable for params that I get off of the ActivatedRoute into a promise but haven't had any luck. I have been able to turn http requests into promises successfully:

this.riaService.getAdvisorsForState(this.activeState)
    .then(rias => {
        this.riasForState = rias.json();
        console.log(this.riasForState);
    });
// this all works ^

But I have not been able to turn the 'activatedRoute.params' into a promise:

export class InvestmentAdvisorStateComponent implements OnInit {

    constructor(private activatedRoute: ActivatedRoute, private riaService: InvestmentAdvisorService) { }

    getStateFromUrl() {
        this.activatedRoute.params
            .toPromise()
            .then(params => {
                console.log('HERE',params)
            });
    }
// this does not work for me ^

This is what I have right now:

getStateFromUrl() {
    this.activatedRoute.params
        .subscribe((param: any) => {
            this.activeState = param['state'];
        });
}
// works ^

I am hoping to implement this as a promise thought so I can .then off of it. Any help on how to do this is greatly appreciated.

Share Improve this question edited Oct 6, 2016 at 17:34 georgej asked Oct 6, 2016 at 17:27 georgejgeorgej 3,3216 gold badges25 silver badges46 bronze badges 7
  • What behavior do you expect? This promise probably only pletes when you navigate away from that route while http.get() pletes when the response from the server arrives. – Günter Zöchbauer Commented Oct 6, 2016 at 17:29
  • @GünterZöchbauer Oh, I didnt realize that. I edited my post to show the functionality that I have right now with .subscribe. I am trying to turn my observable into a promise so I can chain it with .then – georgej Commented Oct 6, 2016 at 17:35
  • Why not just with .map() – Günter Zöchbauer Commented Oct 6, 2016 at 17:50
  • hmm.. that didnt allow me to turn it into a promise – georgej Commented Oct 6, 2016 at 20:33
  • If you turn it into a promise, you defeat the purpose of using the "live" route. You might as well just use the snapshot. – Paul Samsotha Commented Oct 7, 2016 at 1:39
 |  Show 2 more ments

1 Answer 1

Reset to default 9

The main difference between observable and promise is, that observable can emit multiple events while a promise only emits a single event.

To make your example work, I assume you are only interested in the first event.

getStateFromUrl() {
    this.activatedRoute.params
        .first()
        .toPromise()
        .then(params => {
            console.log('HERE',params)
        });
}

This way the promise created with toPromise() is pleted with the first params event.

发布评论

评论列表(0)

  1. 暂无评论