Subscribe on route.queryParams and route.params at the same time










1















I have next URL with both route.params and route.queryParams



.../search/VALUE1?method=VALUE2


I have created route




path : 'search/:id',
component: MyComponent



Component



export class MyComponent {
...
constructor(private route: ActivatedRoute)
ngOnInit()
combineLatest(this.route.queryParams, this.route.params)
.subscribe([queryParams, params] =>
console.log(ID:' + params('id') + ',M:' +queryParams('method'));
// code
)



But there is a problem - when user changes main param and query param at the same time - then subscribtion will be called twice, e.g.
user calls



.../search/VALUE1?method=VALUE2
.../search/VALUE3?method=VALUE4


Console log



ID:VALUE1,M:VALUE2
ID:VALUE3,M:VALUE2 <-- redundant method call
ID:VALUE3,M:VALUE4


How to subscribe on both route.params, route.queryParams but call subscribtion only once?



What tried



 private method: string;
//
ngOnInit()
this.route.queryParams.subscribe(
queryParams =>
this.method = queryParams['method'];
);

this.route.params.subscribe(
params =>
console.log(ID:' + params('id') + ',M:' + this.method);
// code





And this works perfectly.
But can I be sure that this.method will contain correct value at the subscribtion?










share|improve this question
























  • Tried getting them separately?

    – Arcteezy
    Nov 13 '18 at 9:54











  • You can try experimenting with other rxjs operators, like filter

    – hevans900
    Nov 13 '18 at 9:55











  • @Arcteezy I have added code what I tried. And this works. But can I be sure that this.method will contain correct value at the subscribtion?

    – Ilya
    Nov 13 '18 at 9:59















1















I have next URL with both route.params and route.queryParams



.../search/VALUE1?method=VALUE2


I have created route




path : 'search/:id',
component: MyComponent



Component



export class MyComponent {
...
constructor(private route: ActivatedRoute)
ngOnInit()
combineLatest(this.route.queryParams, this.route.params)
.subscribe([queryParams, params] =>
console.log(ID:' + params('id') + ',M:' +queryParams('method'));
// code
)



But there is a problem - when user changes main param and query param at the same time - then subscribtion will be called twice, e.g.
user calls



.../search/VALUE1?method=VALUE2
.../search/VALUE3?method=VALUE4


Console log



ID:VALUE1,M:VALUE2
ID:VALUE3,M:VALUE2 <-- redundant method call
ID:VALUE3,M:VALUE4


How to subscribe on both route.params, route.queryParams but call subscribtion only once?



What tried



 private method: string;
//
ngOnInit()
this.route.queryParams.subscribe(
queryParams =>
this.method = queryParams['method'];
);

this.route.params.subscribe(
params =>
console.log(ID:' + params('id') + ',M:' + this.method);
// code





And this works perfectly.
But can I be sure that this.method will contain correct value at the subscribtion?










share|improve this question
























  • Tried getting them separately?

    – Arcteezy
    Nov 13 '18 at 9:54











  • You can try experimenting with other rxjs operators, like filter

    – hevans900
    Nov 13 '18 at 9:55











  • @Arcteezy I have added code what I tried. And this works. But can I be sure that this.method will contain correct value at the subscribtion?

    – Ilya
    Nov 13 '18 at 9:59













1












1








1








I have next URL with both route.params and route.queryParams



.../search/VALUE1?method=VALUE2


I have created route




path : 'search/:id',
component: MyComponent



Component



export class MyComponent {
...
constructor(private route: ActivatedRoute)
ngOnInit()
combineLatest(this.route.queryParams, this.route.params)
.subscribe([queryParams, params] =>
console.log(ID:' + params('id') + ',M:' +queryParams('method'));
// code
)



But there is a problem - when user changes main param and query param at the same time - then subscribtion will be called twice, e.g.
user calls



.../search/VALUE1?method=VALUE2
.../search/VALUE3?method=VALUE4


Console log



ID:VALUE1,M:VALUE2
ID:VALUE3,M:VALUE2 <-- redundant method call
ID:VALUE3,M:VALUE4


How to subscribe on both route.params, route.queryParams but call subscribtion only once?



What tried



 private method: string;
//
ngOnInit()
this.route.queryParams.subscribe(
queryParams =>
this.method = queryParams['method'];
);

this.route.params.subscribe(
params =>
console.log(ID:' + params('id') + ',M:' + this.method);
// code





And this works perfectly.
But can I be sure that this.method will contain correct value at the subscribtion?










share|improve this question
















I have next URL with both route.params and route.queryParams



.../search/VALUE1?method=VALUE2


I have created route




path : 'search/:id',
component: MyComponent



Component



export class MyComponent {
...
constructor(private route: ActivatedRoute)
ngOnInit()
combineLatest(this.route.queryParams, this.route.params)
.subscribe([queryParams, params] =>
console.log(ID:' + params('id') + ',M:' +queryParams('method'));
// code
)



But there is a problem - when user changes main param and query param at the same time - then subscribtion will be called twice, e.g.
user calls



.../search/VALUE1?method=VALUE2
.../search/VALUE3?method=VALUE4


Console log



ID:VALUE1,M:VALUE2
ID:VALUE3,M:VALUE2 <-- redundant method call
ID:VALUE3,M:VALUE4


How to subscribe on both route.params, route.queryParams but call subscribtion only once?



What tried



 private method: string;
//
ngOnInit()
this.route.queryParams.subscribe(
queryParams =>
this.method = queryParams['method'];
);

this.route.params.subscribe(
params =>
console.log(ID:' + params('id') + ',M:' + this.method);
// code





And this works perfectly.
But can I be sure that this.method will contain correct value at the subscribtion?







angular






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:58







Ilya

















asked Nov 13 '18 at 9:48









IlyaIlya

23.2k1891141




23.2k1891141












  • Tried getting them separately?

    – Arcteezy
    Nov 13 '18 at 9:54











  • You can try experimenting with other rxjs operators, like filter

    – hevans900
    Nov 13 '18 at 9:55











  • @Arcteezy I have added code what I tried. And this works. But can I be sure that this.method will contain correct value at the subscribtion?

    – Ilya
    Nov 13 '18 at 9:59

















  • Tried getting them separately?

    – Arcteezy
    Nov 13 '18 at 9:54











  • You can try experimenting with other rxjs operators, like filter

    – hevans900
    Nov 13 '18 at 9:55











  • @Arcteezy I have added code what I tried. And this works. But can I be sure that this.method will contain correct value at the subscribtion?

    – Ilya
    Nov 13 '18 at 9:59
















Tried getting them separately?

– Arcteezy
Nov 13 '18 at 9:54





Tried getting them separately?

– Arcteezy
Nov 13 '18 at 9:54













You can try experimenting with other rxjs operators, like filter

– hevans900
Nov 13 '18 at 9:55





You can try experimenting with other rxjs operators, like filter

– hevans900
Nov 13 '18 at 9:55













@Arcteezy I have added code what I tried. And this works. But can I be sure that this.method will contain correct value at the subscribtion?

– Ilya
Nov 13 '18 at 9:59





@Arcteezy I have added code what I tried. And this works. But can I be sure that this.method will contain correct value at the subscribtion?

– Ilya
Nov 13 '18 at 9:59












1 Answer
1






active

oldest

votes


















0














For getting params, you dont need subscription. The one below works fine when changing both.



ngOnInit() 

this.route.queryParams.subscribe( p =>

console.log("ID :" + this.route.snapshot.params['id'] + ',M:' + p['method'],)

)






share|improve this answer























  • Doesn't work when queryParam is not changed, e.g. .../search/VALUE1?method=VALUE2 .../search/VALUE3?method=VALUE2 log will not be called after second http call

    – Ilya
    Nov 13 '18 at 10:29












  • Worked for me. Can you provide a live demo?

    – Arcteezy
    Nov 13 '18 at 11:16










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278156%2fsubscribe-on-route-queryparams-and-route-params-at-the-same-time%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














For getting params, you dont need subscription. The one below works fine when changing both.



ngOnInit() 

this.route.queryParams.subscribe( p =>

console.log("ID :" + this.route.snapshot.params['id'] + ',M:' + p['method'],)

)






share|improve this answer























  • Doesn't work when queryParam is not changed, e.g. .../search/VALUE1?method=VALUE2 .../search/VALUE3?method=VALUE2 log will not be called after second http call

    – Ilya
    Nov 13 '18 at 10:29












  • Worked for me. Can you provide a live demo?

    – Arcteezy
    Nov 13 '18 at 11:16















0














For getting params, you dont need subscription. The one below works fine when changing both.



ngOnInit() 

this.route.queryParams.subscribe( p =>

console.log("ID :" + this.route.snapshot.params['id'] + ',M:' + p['method'],)

)






share|improve this answer























  • Doesn't work when queryParam is not changed, e.g. .../search/VALUE1?method=VALUE2 .../search/VALUE3?method=VALUE2 log will not be called after second http call

    – Ilya
    Nov 13 '18 at 10:29












  • Worked for me. Can you provide a live demo?

    – Arcteezy
    Nov 13 '18 at 11:16













0












0








0







For getting params, you dont need subscription. The one below works fine when changing both.



ngOnInit() 

this.route.queryParams.subscribe( p =>

console.log("ID :" + this.route.snapshot.params['id'] + ',M:' + p['method'],)

)






share|improve this answer













For getting params, you dont need subscription. The one below works fine when changing both.



ngOnInit() 

this.route.queryParams.subscribe( p =>

console.log("ID :" + this.route.snapshot.params['id'] + ',M:' + p['method'],)

)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 10:20









ArcteezyArcteezy

7619




7619












  • Doesn't work when queryParam is not changed, e.g. .../search/VALUE1?method=VALUE2 .../search/VALUE3?method=VALUE2 log will not be called after second http call

    – Ilya
    Nov 13 '18 at 10:29












  • Worked for me. Can you provide a live demo?

    – Arcteezy
    Nov 13 '18 at 11:16

















  • Doesn't work when queryParam is not changed, e.g. .../search/VALUE1?method=VALUE2 .../search/VALUE3?method=VALUE2 log will not be called after second http call

    – Ilya
    Nov 13 '18 at 10:29












  • Worked for me. Can you provide a live demo?

    – Arcteezy
    Nov 13 '18 at 11:16
















Doesn't work when queryParam is not changed, e.g. .../search/VALUE1?method=VALUE2 .../search/VALUE3?method=VALUE2 log will not be called after second http call

– Ilya
Nov 13 '18 at 10:29






Doesn't work when queryParam is not changed, e.g. .../search/VALUE1?method=VALUE2 .../search/VALUE3?method=VALUE2 log will not be called after second http call

– Ilya
Nov 13 '18 at 10:29














Worked for me. Can you provide a live demo?

– Arcteezy
Nov 13 '18 at 11:16





Worked for me. Can you provide a live demo?

– Arcteezy
Nov 13 '18 at 11:16

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278156%2fsubscribe-on-route-queryparams-and-route-params-at-the-same-time%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto