Angular 6+ routing issue when trying to change to components
I've got a search box drop-down that when you type in a query returns a list of results. Clicking on a result takes you off to that component to view. This works as expected when you use the search from say the homepage. As soon as you try and use the search from where the search takes you, the URL in the browser changes however the content doesn't.
I think this is just beginners mistake with how routing works but I can't for the life of me work out what I've done wrong.
search-component.html
<div *ngIf="hasSearchResults">
<div class="search-results">
<ul>
<li *ngFor="let video of searchResults" class="search-results-item">
<div class="columns" (click)="goToSearch(video.id)">
<div class="column is-narrow">
<figure class="image is-32x32">
<div *ngIf="video.cover;else noThumbnail">
<img src="http://localhost:8080/videos/t/{{video.cover.id}}" class="rounded">
</div>
<ng-template #noThumbnail></ng-template>
</figure>
</div>
<div class="column">
<p>{{video.name}}</p>
<p class="metadata">{{video.absolutePath}}</p>
</div>
</div>
</li>
</ul>
</div>
</div>
search-component.ts (relivant part)
public goToSearch(id: string) {
this.router.navigate([`videos/${id}`]);
}
Now this all works from anywhere in the site, as long as its not on the video component.
app.routing.ts
const routes: Routes = [
{path: 'videos', component: VideoListComponent},
{path: "videos/:id", component: VideoItemComponent}
];
I suspect the issue is I have just one router-outlet on the 'main' html file.
app.component.html
<div class="section">
<div class="columns">
<div class="column is-narrow">
<app-sidebar></app-sidebar>
</div>
<div class="column is-main-content">
<router-outlet></router-outlet>
</div>
</div>
</div>
What have I done wrong here?
angular angular2-routing
add a comment |
I've got a search box drop-down that when you type in a query returns a list of results. Clicking on a result takes you off to that component to view. This works as expected when you use the search from say the homepage. As soon as you try and use the search from where the search takes you, the URL in the browser changes however the content doesn't.
I think this is just beginners mistake with how routing works but I can't for the life of me work out what I've done wrong.
search-component.html
<div *ngIf="hasSearchResults">
<div class="search-results">
<ul>
<li *ngFor="let video of searchResults" class="search-results-item">
<div class="columns" (click)="goToSearch(video.id)">
<div class="column is-narrow">
<figure class="image is-32x32">
<div *ngIf="video.cover;else noThumbnail">
<img src="http://localhost:8080/videos/t/{{video.cover.id}}" class="rounded">
</div>
<ng-template #noThumbnail></ng-template>
</figure>
</div>
<div class="column">
<p>{{video.name}}</p>
<p class="metadata">{{video.absolutePath}}</p>
</div>
</div>
</li>
</ul>
</div>
</div>
search-component.ts (relivant part)
public goToSearch(id: string) {
this.router.navigate([`videos/${id}`]);
}
Now this all works from anywhere in the site, as long as its not on the video component.
app.routing.ts
const routes: Routes = [
{path: 'videos', component: VideoListComponent},
{path: "videos/:id", component: VideoItemComponent}
];
I suspect the issue is I have just one router-outlet on the 'main' html file.
app.component.html
<div class="section">
<div class="columns">
<div class="column is-narrow">
<app-sidebar></app-sidebar>
</div>
<div class="column is-main-content">
<router-outlet></router-outlet>
</div>
</div>
</div>
What have I done wrong here?
angular angular2-routing
your routing looks fine to me. what you mean by 'content doesn't'? are u able to navigate the page you want?
– Code-EZ
Nov 12 at 4:37
Sorry I know that isn't very clear. If I've gone to a search result (a video) and search again, when clicking on the new search result the URL changes to the correct ID of the video, however I'm still sitting at the old search result.
– Chris Turner
Nov 12 at 4:42
Where did you put router outlet? Show more code about it.
– wannadream
Nov 12 at 4:46
Did you try changing the order of the elements in theroutes
array? The default matching algo' is prefix matching, so any route that starts withvideos
will get matched to the first.
– Aviad P.
Nov 12 at 5:06
add a comment |
I've got a search box drop-down that when you type in a query returns a list of results. Clicking on a result takes you off to that component to view. This works as expected when you use the search from say the homepage. As soon as you try and use the search from where the search takes you, the URL in the browser changes however the content doesn't.
I think this is just beginners mistake with how routing works but I can't for the life of me work out what I've done wrong.
search-component.html
<div *ngIf="hasSearchResults">
<div class="search-results">
<ul>
<li *ngFor="let video of searchResults" class="search-results-item">
<div class="columns" (click)="goToSearch(video.id)">
<div class="column is-narrow">
<figure class="image is-32x32">
<div *ngIf="video.cover;else noThumbnail">
<img src="http://localhost:8080/videos/t/{{video.cover.id}}" class="rounded">
</div>
<ng-template #noThumbnail></ng-template>
</figure>
</div>
<div class="column">
<p>{{video.name}}</p>
<p class="metadata">{{video.absolutePath}}</p>
</div>
</div>
</li>
</ul>
</div>
</div>
search-component.ts (relivant part)
public goToSearch(id: string) {
this.router.navigate([`videos/${id}`]);
}
Now this all works from anywhere in the site, as long as its not on the video component.
app.routing.ts
const routes: Routes = [
{path: 'videos', component: VideoListComponent},
{path: "videos/:id", component: VideoItemComponent}
];
I suspect the issue is I have just one router-outlet on the 'main' html file.
app.component.html
<div class="section">
<div class="columns">
<div class="column is-narrow">
<app-sidebar></app-sidebar>
</div>
<div class="column is-main-content">
<router-outlet></router-outlet>
</div>
</div>
</div>
What have I done wrong here?
angular angular2-routing
I've got a search box drop-down that when you type in a query returns a list of results. Clicking on a result takes you off to that component to view. This works as expected when you use the search from say the homepage. As soon as you try and use the search from where the search takes you, the URL in the browser changes however the content doesn't.
I think this is just beginners mistake with how routing works but I can't for the life of me work out what I've done wrong.
search-component.html
<div *ngIf="hasSearchResults">
<div class="search-results">
<ul>
<li *ngFor="let video of searchResults" class="search-results-item">
<div class="columns" (click)="goToSearch(video.id)">
<div class="column is-narrow">
<figure class="image is-32x32">
<div *ngIf="video.cover;else noThumbnail">
<img src="http://localhost:8080/videos/t/{{video.cover.id}}" class="rounded">
</div>
<ng-template #noThumbnail></ng-template>
</figure>
</div>
<div class="column">
<p>{{video.name}}</p>
<p class="metadata">{{video.absolutePath}}</p>
</div>
</div>
</li>
</ul>
</div>
</div>
search-component.ts (relivant part)
public goToSearch(id: string) {
this.router.navigate([`videos/${id}`]);
}
Now this all works from anywhere in the site, as long as its not on the video component.
app.routing.ts
const routes: Routes = [
{path: 'videos', component: VideoListComponent},
{path: "videos/:id", component: VideoItemComponent}
];
I suspect the issue is I have just one router-outlet on the 'main' html file.
app.component.html
<div class="section">
<div class="columns">
<div class="column is-narrow">
<app-sidebar></app-sidebar>
</div>
<div class="column is-main-content">
<router-outlet></router-outlet>
</div>
</div>
</div>
What have I done wrong here?
angular angular2-routing
angular angular2-routing
edited Nov 12 at 4:48
asked Nov 12 at 4:24
Chris Turner
86811029
86811029
your routing looks fine to me. what you mean by 'content doesn't'? are u able to navigate the page you want?
– Code-EZ
Nov 12 at 4:37
Sorry I know that isn't very clear. If I've gone to a search result (a video) and search again, when clicking on the new search result the URL changes to the correct ID of the video, however I'm still sitting at the old search result.
– Chris Turner
Nov 12 at 4:42
Where did you put router outlet? Show more code about it.
– wannadream
Nov 12 at 4:46
Did you try changing the order of the elements in theroutes
array? The default matching algo' is prefix matching, so any route that starts withvideos
will get matched to the first.
– Aviad P.
Nov 12 at 5:06
add a comment |
your routing looks fine to me. what you mean by 'content doesn't'? are u able to navigate the page you want?
– Code-EZ
Nov 12 at 4:37
Sorry I know that isn't very clear. If I've gone to a search result (a video) and search again, when clicking on the new search result the URL changes to the correct ID of the video, however I'm still sitting at the old search result.
– Chris Turner
Nov 12 at 4:42
Where did you put router outlet? Show more code about it.
– wannadream
Nov 12 at 4:46
Did you try changing the order of the elements in theroutes
array? The default matching algo' is prefix matching, so any route that starts withvideos
will get matched to the first.
– Aviad P.
Nov 12 at 5:06
your routing looks fine to me. what you mean by 'content doesn't'? are u able to navigate the page you want?
– Code-EZ
Nov 12 at 4:37
your routing looks fine to me. what you mean by 'content doesn't'? are u able to navigate the page you want?
– Code-EZ
Nov 12 at 4:37
Sorry I know that isn't very clear. If I've gone to a search result (a video) and search again, when clicking on the new search result the URL changes to the correct ID of the video, however I'm still sitting at the old search result.
– Chris Turner
Nov 12 at 4:42
Sorry I know that isn't very clear. If I've gone to a search result (a video) and search again, when clicking on the new search result the URL changes to the correct ID of the video, however I'm still sitting at the old search result.
– Chris Turner
Nov 12 at 4:42
Where did you put router outlet? Show more code about it.
– wannadream
Nov 12 at 4:46
Where did you put router outlet? Show more code about it.
– wannadream
Nov 12 at 4:46
Did you try changing the order of the elements in the
routes
array? The default matching algo' is prefix matching, so any route that starts with videos
will get matched to the first.– Aviad P.
Nov 12 at 5:06
Did you try changing the order of the elements in the
routes
array? The default matching algo' is prefix matching, so any route that starts with videos
will get matched to the first.– Aviad P.
Nov 12 at 5:06
add a comment |
1 Answer
1
active
oldest
votes
I am not able to comment, SO I am adding it as an answer.
All your routing setup is correct. The reason why it works when you are on homepage is because the url(videos/:id) is new and its get activated for the first time by angular router.
Since you are using dynamic routing,(i.e only the value of the id changes), the component is gonna remain the same.(i.e this component has been already activated and is not loaded twice). So I believe you have react to url changes and pass/set the values in your VideoItemComponent.
Champ, that worked a treat.
– Chris Turner
Nov 12 at 5:18
Awesome. Glad to help
– KiraAG
Nov 12 at 5:24
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255950%2fangular-6-routing-issue-when-trying-to-change-to-components%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
I am not able to comment, SO I am adding it as an answer.
All your routing setup is correct. The reason why it works when you are on homepage is because the url(videos/:id) is new and its get activated for the first time by angular router.
Since you are using dynamic routing,(i.e only the value of the id changes), the component is gonna remain the same.(i.e this component has been already activated and is not loaded twice). So I believe you have react to url changes and pass/set the values in your VideoItemComponent.
Champ, that worked a treat.
– Chris Turner
Nov 12 at 5:18
Awesome. Glad to help
– KiraAG
Nov 12 at 5:24
add a comment |
I am not able to comment, SO I am adding it as an answer.
All your routing setup is correct. The reason why it works when you are on homepage is because the url(videos/:id) is new and its get activated for the first time by angular router.
Since you are using dynamic routing,(i.e only the value of the id changes), the component is gonna remain the same.(i.e this component has been already activated and is not loaded twice). So I believe you have react to url changes and pass/set the values in your VideoItemComponent.
Champ, that worked a treat.
– Chris Turner
Nov 12 at 5:18
Awesome. Glad to help
– KiraAG
Nov 12 at 5:24
add a comment |
I am not able to comment, SO I am adding it as an answer.
All your routing setup is correct. The reason why it works when you are on homepage is because the url(videos/:id) is new and its get activated for the first time by angular router.
Since you are using dynamic routing,(i.e only the value of the id changes), the component is gonna remain the same.(i.e this component has been already activated and is not loaded twice). So I believe you have react to url changes and pass/set the values in your VideoItemComponent.
I am not able to comment, SO I am adding it as an answer.
All your routing setup is correct. The reason why it works when you are on homepage is because the url(videos/:id) is new and its get activated for the first time by angular router.
Since you are using dynamic routing,(i.e only the value of the id changes), the component is gonna remain the same.(i.e this component has been already activated and is not loaded twice). So I believe you have react to url changes and pass/set the values in your VideoItemComponent.
answered Nov 12 at 5:09
KiraAG
1598
1598
Champ, that worked a treat.
– Chris Turner
Nov 12 at 5:18
Awesome. Glad to help
– KiraAG
Nov 12 at 5:24
add a comment |
Champ, that worked a treat.
– Chris Turner
Nov 12 at 5:18
Awesome. Glad to help
– KiraAG
Nov 12 at 5:24
Champ, that worked a treat.
– Chris Turner
Nov 12 at 5:18
Champ, that worked a treat.
– Chris Turner
Nov 12 at 5:18
Awesome. Glad to help
– KiraAG
Nov 12 at 5:24
Awesome. Glad to help
– KiraAG
Nov 12 at 5:24
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255950%2fangular-6-routing-issue-when-trying-to-change-to-components%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
your routing looks fine to me. what you mean by 'content doesn't'? are u able to navigate the page you want?
– Code-EZ
Nov 12 at 4:37
Sorry I know that isn't very clear. If I've gone to a search result (a video) and search again, when clicking on the new search result the URL changes to the correct ID of the video, however I'm still sitting at the old search result.
– Chris Turner
Nov 12 at 4:42
Where did you put router outlet? Show more code about it.
– wannadream
Nov 12 at 4:46
Did you try changing the order of the elements in the
routes
array? The default matching algo' is prefix matching, so any route that starts withvideos
will get matched to the first.– Aviad P.
Nov 12 at 5:06