How to re-load route child-components after URL segment changed?












0















I am having routes which are the same for different projects. So everything shown depends just on :projectId:



  {path: 'projects/:projecteId', children: [
{path: 'info', component: InfoComponent},
{path: 'settings', component: SettingsComponent},

], canActivate: [ProjectGuard]},

// Other routes ..
{path: 'account', component: AccountComponent]


In my toolbar I am having an option to select a project - similar to what you can do in the GAE console:



enter image description here



I would like to know how I can make a change in projectId detectable and update all affected components.



As you can see I have a ProjectGuard - the idea would be to have a guard that observes an object provided by a ProjectService and applies changes whenever the selection changes. However, I am not sure if this is a viable solution and whether there is a better, more Angular-ish way, to do something like that.



Do I really need such a guard here?










share|improve this question























  • Which component do you load for projects/:projecteId path?

    – SiddAjmera
    Nov 25 '18 at 12:51






  • 1





    If you navigate to a different project, the ActivatedRoutes will emit new params, and you can thus observe the params change from your components. Not sure if that's what you're asking. You can also tweak the RouteReuseStrategy: angular.io/api/router/RouteReuseStrategy#shouldReuseRoute

    – JB Nizet
    Nov 25 '18 at 12:52
















0















I am having routes which are the same for different projects. So everything shown depends just on :projectId:



  {path: 'projects/:projecteId', children: [
{path: 'info', component: InfoComponent},
{path: 'settings', component: SettingsComponent},

], canActivate: [ProjectGuard]},

// Other routes ..
{path: 'account', component: AccountComponent]


In my toolbar I am having an option to select a project - similar to what you can do in the GAE console:



enter image description here



I would like to know how I can make a change in projectId detectable and update all affected components.



As you can see I have a ProjectGuard - the idea would be to have a guard that observes an object provided by a ProjectService and applies changes whenever the selection changes. However, I am not sure if this is a viable solution and whether there is a better, more Angular-ish way, to do something like that.



Do I really need such a guard here?










share|improve this question























  • Which component do you load for projects/:projecteId path?

    – SiddAjmera
    Nov 25 '18 at 12:51






  • 1





    If you navigate to a different project, the ActivatedRoutes will emit new params, and you can thus observe the params change from your components. Not sure if that's what you're asking. You can also tweak the RouteReuseStrategy: angular.io/api/router/RouteReuseStrategy#shouldReuseRoute

    – JB Nizet
    Nov 25 '18 at 12:52














0












0








0








I am having routes which are the same for different projects. So everything shown depends just on :projectId:



  {path: 'projects/:projecteId', children: [
{path: 'info', component: InfoComponent},
{path: 'settings', component: SettingsComponent},

], canActivate: [ProjectGuard]},

// Other routes ..
{path: 'account', component: AccountComponent]


In my toolbar I am having an option to select a project - similar to what you can do in the GAE console:



enter image description here



I would like to know how I can make a change in projectId detectable and update all affected components.



As you can see I have a ProjectGuard - the idea would be to have a guard that observes an object provided by a ProjectService and applies changes whenever the selection changes. However, I am not sure if this is a viable solution and whether there is a better, more Angular-ish way, to do something like that.



Do I really need such a guard here?










share|improve this question














I am having routes which are the same for different projects. So everything shown depends just on :projectId:



  {path: 'projects/:projecteId', children: [
{path: 'info', component: InfoComponent},
{path: 'settings', component: SettingsComponent},

], canActivate: [ProjectGuard]},

// Other routes ..
{path: 'account', component: AccountComponent]


In my toolbar I am having an option to select a project - similar to what you can do in the GAE console:



enter image description here



I would like to know how I can make a change in projectId detectable and update all affected components.



As you can see I have a ProjectGuard - the idea would be to have a guard that observes an object provided by a ProjectService and applies changes whenever the selection changes. However, I am not sure if this is a viable solution and whether there is a better, more Angular-ish way, to do something like that.



Do I really need such a guard here?







angular typescript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 12:42









displaynamedisplayname

6,6301783175




6,6301783175













  • Which component do you load for projects/:projecteId path?

    – SiddAjmera
    Nov 25 '18 at 12:51






  • 1





    If you navigate to a different project, the ActivatedRoutes will emit new params, and you can thus observe the params change from your components. Not sure if that's what you're asking. You can also tweak the RouteReuseStrategy: angular.io/api/router/RouteReuseStrategy#shouldReuseRoute

    – JB Nizet
    Nov 25 '18 at 12:52



















  • Which component do you load for projects/:projecteId path?

    – SiddAjmera
    Nov 25 '18 at 12:51






  • 1





    If you navigate to a different project, the ActivatedRoutes will emit new params, and you can thus observe the params change from your components. Not sure if that's what you're asking. You can also tweak the RouteReuseStrategy: angular.io/api/router/RouteReuseStrategy#shouldReuseRoute

    – JB Nizet
    Nov 25 '18 at 12:52

















Which component do you load for projects/:projecteId path?

– SiddAjmera
Nov 25 '18 at 12:51





Which component do you load for projects/:projecteId path?

– SiddAjmera
Nov 25 '18 at 12:51




1




1





If you navigate to a different project, the ActivatedRoutes will emit new params, and you can thus observe the params change from your components. Not sure if that's what you're asking. You can also tweak the RouteReuseStrategy: angular.io/api/router/RouteReuseStrategy#shouldReuseRoute

– JB Nizet
Nov 25 '18 at 12:52





If you navigate to a different project, the ActivatedRoutes will emit new params, and you can thus observe the params change from your components. Not sure if that's what you're asking. You can also tweak the RouteReuseStrategy: angular.io/api/router/RouteReuseStrategy#shouldReuseRoute

– JB Nizet
Nov 25 '18 at 12:52












1 Answer
1






active

oldest

votes


















0














In the component that you'll load for path: 'projects/:projecteId', you can inject the ActivatedRoute and listen to params Observable on it.



The change in params will run your code:



Let's say that you open a ProjectComponent, you can do something like:



import { ActivatedRoute } from '@angular/router';

@Component({...})
export class ProjectComponent {
projectId;
constructor(private route: ActivatedRoute) {}

ngOnInit() {
this.route.params.subscribe(params => {
this.projectId = params['projecteId'];
// Here you'll always get notified when `projecteId` changes.
// You can do the needful from here.
});
}

}


Update:



In case of a CanActivate guard, inside the canActivate you can use the ActivatedRouteSnapshot. That also has a params on it which is an Object but with the updated params



import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class ProjectGuard implements CanActivate {
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
// Here the `next.params.projectId` will always give you updated projectId
...
}
}


You can watch for params.projectId and then also use some sort of a service that would store the previous value of the projectId. This you could use to compare with the current value of projectId to check if the Project was actually changed from the dropdown. You can then inject that service as a dependency in the ProjectGuard and you'll be good to go.





Here's a Sample StackBlitz for your ref.






share|improve this answer


























  • Well, I do not have a ProjectComponent at this point. I went with a guard as you can take from my question. I don't think I need an entire component for this.

    – displayname
    Nov 25 '18 at 13:11











  • Inside the guard params is always an empty dictionary ({}) O_o

    – displayname
    Nov 25 '18 at 13:17













  • Well in that case, since you're using it in a CanActivate guard, you'll get the ActivatedRouteSnapshot as the first arg. to the canActivate method. Even that has params on it that you can subscribe to, to get the value. I've updated my answer accordingly.

    – SiddAjmera
    Nov 25 '18 at 13:21











  • params is not an observable though :/

    – displayname
    Nov 25 '18 at 13:24











  • Oh and actually - the whole subscribing thing will nevere work since the route will nevere change until I updated it. The whole point is to detect a condition, "user changed project" and then navigate to another route. I'm afraid you misunderstood what I am looking for.

    – displayname
    Nov 25 '18 at 13:27













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%2f53467544%2fhow-to-re-load-route-child-components-after-url-segment-changed%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














In the component that you'll load for path: 'projects/:projecteId', you can inject the ActivatedRoute and listen to params Observable on it.



The change in params will run your code:



Let's say that you open a ProjectComponent, you can do something like:



import { ActivatedRoute } from '@angular/router';

@Component({...})
export class ProjectComponent {
projectId;
constructor(private route: ActivatedRoute) {}

ngOnInit() {
this.route.params.subscribe(params => {
this.projectId = params['projecteId'];
// Here you'll always get notified when `projecteId` changes.
// You can do the needful from here.
});
}

}


Update:



In case of a CanActivate guard, inside the canActivate you can use the ActivatedRouteSnapshot. That also has a params on it which is an Object but with the updated params



import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class ProjectGuard implements CanActivate {
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
// Here the `next.params.projectId` will always give you updated projectId
...
}
}


You can watch for params.projectId and then also use some sort of a service that would store the previous value of the projectId. This you could use to compare with the current value of projectId to check if the Project was actually changed from the dropdown. You can then inject that service as a dependency in the ProjectGuard and you'll be good to go.





Here's a Sample StackBlitz for your ref.






share|improve this answer


























  • Well, I do not have a ProjectComponent at this point. I went with a guard as you can take from my question. I don't think I need an entire component for this.

    – displayname
    Nov 25 '18 at 13:11











  • Inside the guard params is always an empty dictionary ({}) O_o

    – displayname
    Nov 25 '18 at 13:17













  • Well in that case, since you're using it in a CanActivate guard, you'll get the ActivatedRouteSnapshot as the first arg. to the canActivate method. Even that has params on it that you can subscribe to, to get the value. I've updated my answer accordingly.

    – SiddAjmera
    Nov 25 '18 at 13:21











  • params is not an observable though :/

    – displayname
    Nov 25 '18 at 13:24











  • Oh and actually - the whole subscribing thing will nevere work since the route will nevere change until I updated it. The whole point is to detect a condition, "user changed project" and then navigate to another route. I'm afraid you misunderstood what I am looking for.

    – displayname
    Nov 25 '18 at 13:27


















0














In the component that you'll load for path: 'projects/:projecteId', you can inject the ActivatedRoute and listen to params Observable on it.



The change in params will run your code:



Let's say that you open a ProjectComponent, you can do something like:



import { ActivatedRoute } from '@angular/router';

@Component({...})
export class ProjectComponent {
projectId;
constructor(private route: ActivatedRoute) {}

ngOnInit() {
this.route.params.subscribe(params => {
this.projectId = params['projecteId'];
// Here you'll always get notified when `projecteId` changes.
// You can do the needful from here.
});
}

}


Update:



In case of a CanActivate guard, inside the canActivate you can use the ActivatedRouteSnapshot. That also has a params on it which is an Object but with the updated params



import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class ProjectGuard implements CanActivate {
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
// Here the `next.params.projectId` will always give you updated projectId
...
}
}


You can watch for params.projectId and then also use some sort of a service that would store the previous value of the projectId. This you could use to compare with the current value of projectId to check if the Project was actually changed from the dropdown. You can then inject that service as a dependency in the ProjectGuard and you'll be good to go.





Here's a Sample StackBlitz for your ref.






share|improve this answer


























  • Well, I do not have a ProjectComponent at this point. I went with a guard as you can take from my question. I don't think I need an entire component for this.

    – displayname
    Nov 25 '18 at 13:11











  • Inside the guard params is always an empty dictionary ({}) O_o

    – displayname
    Nov 25 '18 at 13:17













  • Well in that case, since you're using it in a CanActivate guard, you'll get the ActivatedRouteSnapshot as the first arg. to the canActivate method. Even that has params on it that you can subscribe to, to get the value. I've updated my answer accordingly.

    – SiddAjmera
    Nov 25 '18 at 13:21











  • params is not an observable though :/

    – displayname
    Nov 25 '18 at 13:24











  • Oh and actually - the whole subscribing thing will nevere work since the route will nevere change until I updated it. The whole point is to detect a condition, "user changed project" and then navigate to another route. I'm afraid you misunderstood what I am looking for.

    – displayname
    Nov 25 '18 at 13:27
















0












0








0







In the component that you'll load for path: 'projects/:projecteId', you can inject the ActivatedRoute and listen to params Observable on it.



The change in params will run your code:



Let's say that you open a ProjectComponent, you can do something like:



import { ActivatedRoute } from '@angular/router';

@Component({...})
export class ProjectComponent {
projectId;
constructor(private route: ActivatedRoute) {}

ngOnInit() {
this.route.params.subscribe(params => {
this.projectId = params['projecteId'];
// Here you'll always get notified when `projecteId` changes.
// You can do the needful from here.
});
}

}


Update:



In case of a CanActivate guard, inside the canActivate you can use the ActivatedRouteSnapshot. That also has a params on it which is an Object but with the updated params



import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class ProjectGuard implements CanActivate {
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
// Here the `next.params.projectId` will always give you updated projectId
...
}
}


You can watch for params.projectId and then also use some sort of a service that would store the previous value of the projectId. This you could use to compare with the current value of projectId to check if the Project was actually changed from the dropdown. You can then inject that service as a dependency in the ProjectGuard and you'll be good to go.





Here's a Sample StackBlitz for your ref.






share|improve this answer















In the component that you'll load for path: 'projects/:projecteId', you can inject the ActivatedRoute and listen to params Observable on it.



The change in params will run your code:



Let's say that you open a ProjectComponent, you can do something like:



import { ActivatedRoute } from '@angular/router';

@Component({...})
export class ProjectComponent {
projectId;
constructor(private route: ActivatedRoute) {}

ngOnInit() {
this.route.params.subscribe(params => {
this.projectId = params['projecteId'];
// Here you'll always get notified when `projecteId` changes.
// You can do the needful from here.
});
}

}


Update:



In case of a CanActivate guard, inside the canActivate you can use the ActivatedRouteSnapshot. That also has a params on it which is an Object but with the updated params



import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class ProjectGuard implements CanActivate {
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
// Here the `next.params.projectId` will always give you updated projectId
...
}
}


You can watch for params.projectId and then also use some sort of a service that would store the previous value of the projectId. This you could use to compare with the current value of projectId to check if the Project was actually changed from the dropdown. You can then inject that service as a dependency in the ProjectGuard and you'll be good to go.





Here's a Sample StackBlitz for your ref.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 13:38

























answered Nov 25 '18 at 12:56









SiddAjmeraSiddAjmera

15.7k31239




15.7k31239













  • Well, I do not have a ProjectComponent at this point. I went with a guard as you can take from my question. I don't think I need an entire component for this.

    – displayname
    Nov 25 '18 at 13:11











  • Inside the guard params is always an empty dictionary ({}) O_o

    – displayname
    Nov 25 '18 at 13:17













  • Well in that case, since you're using it in a CanActivate guard, you'll get the ActivatedRouteSnapshot as the first arg. to the canActivate method. Even that has params on it that you can subscribe to, to get the value. I've updated my answer accordingly.

    – SiddAjmera
    Nov 25 '18 at 13:21











  • params is not an observable though :/

    – displayname
    Nov 25 '18 at 13:24











  • Oh and actually - the whole subscribing thing will nevere work since the route will nevere change until I updated it. The whole point is to detect a condition, "user changed project" and then navigate to another route. I'm afraid you misunderstood what I am looking for.

    – displayname
    Nov 25 '18 at 13:27





















  • Well, I do not have a ProjectComponent at this point. I went with a guard as you can take from my question. I don't think I need an entire component for this.

    – displayname
    Nov 25 '18 at 13:11











  • Inside the guard params is always an empty dictionary ({}) O_o

    – displayname
    Nov 25 '18 at 13:17













  • Well in that case, since you're using it in a CanActivate guard, you'll get the ActivatedRouteSnapshot as the first arg. to the canActivate method. Even that has params on it that you can subscribe to, to get the value. I've updated my answer accordingly.

    – SiddAjmera
    Nov 25 '18 at 13:21











  • params is not an observable though :/

    – displayname
    Nov 25 '18 at 13:24











  • Oh and actually - the whole subscribing thing will nevere work since the route will nevere change until I updated it. The whole point is to detect a condition, "user changed project" and then navigate to another route. I'm afraid you misunderstood what I am looking for.

    – displayname
    Nov 25 '18 at 13:27



















Well, I do not have a ProjectComponent at this point. I went with a guard as you can take from my question. I don't think I need an entire component for this.

– displayname
Nov 25 '18 at 13:11





Well, I do not have a ProjectComponent at this point. I went with a guard as you can take from my question. I don't think I need an entire component for this.

– displayname
Nov 25 '18 at 13:11













Inside the guard params is always an empty dictionary ({}) O_o

– displayname
Nov 25 '18 at 13:17







Inside the guard params is always an empty dictionary ({}) O_o

– displayname
Nov 25 '18 at 13:17















Well in that case, since you're using it in a CanActivate guard, you'll get the ActivatedRouteSnapshot as the first arg. to the canActivate method. Even that has params on it that you can subscribe to, to get the value. I've updated my answer accordingly.

– SiddAjmera
Nov 25 '18 at 13:21





Well in that case, since you're using it in a CanActivate guard, you'll get the ActivatedRouteSnapshot as the first arg. to the canActivate method. Even that has params on it that you can subscribe to, to get the value. I've updated my answer accordingly.

– SiddAjmera
Nov 25 '18 at 13:21













params is not an observable though :/

– displayname
Nov 25 '18 at 13:24





params is not an observable though :/

– displayname
Nov 25 '18 at 13:24













Oh and actually - the whole subscribing thing will nevere work since the route will nevere change until I updated it. The whole point is to detect a condition, "user changed project" and then navigate to another route. I'm afraid you misunderstood what I am looking for.

– displayname
Nov 25 '18 at 13:27







Oh and actually - the whole subscribing thing will nevere work since the route will nevere change until I updated it. The whole point is to detect a condition, "user changed project" and then navigate to another route. I'm afraid you misunderstood what I am looking for.

– displayname
Nov 25 '18 at 13:27






















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%2f53467544%2fhow-to-re-load-route-child-components-after-url-segment-changed%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







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft