How to use pushState on angular ?
I want to change the url without reloading the page. I use the location service like:
this.location.replaceState(`mynewurl`);
When i navigate back, the history is not good.
I found the html5 pushState solution.
i ask if there is not a service like Location that handles pushState on angular
angular angular-router
add a comment |
I want to change the url without reloading the page. I use the location service like:
this.location.replaceState(`mynewurl`);
When i navigate back, the history is not good.
I found the html5 pushState solution.
i ask if there is not a service like Location that handles pushState on angular
angular angular-router
1
Check ifLocation
is what you're looking for. BTW, if you want to navigate without reloading, why not useRouter
?
– SiddAjmera
Nov 23 '18 at 18:31
I don't know how to do that ???
– drtapha
Nov 23 '18 at 18:38
What exactly are you trying to achieve here? And also, what exactly do you mean by When i navigate back, the history is not good?
– SiddAjmera
Nov 23 '18 at 18:39
I have a route videos and I want when I click on a video, the id of the video is displayed on the url like videos/id without reloading page. I use that id to show an overlay and display the good video
– drtapha
Nov 23 '18 at 18:43
I've added an answer. Check it to see if it helps.
– SiddAjmera
Nov 23 '18 at 19:12
add a comment |
I want to change the url without reloading the page. I use the location service like:
this.location.replaceState(`mynewurl`);
When i navigate back, the history is not good.
I found the html5 pushState solution.
i ask if there is not a service like Location that handles pushState on angular
angular angular-router
I want to change the url without reloading the page. I use the location service like:
this.location.replaceState(`mynewurl`);
When i navigate back, the history is not good.
I found the html5 pushState solution.
i ask if there is not a service like Location that handles pushState on angular
angular angular-router
angular angular-router
asked Nov 23 '18 at 18:30
drtaphadrtapha
1401111
1401111
1
Check ifLocation
is what you're looking for. BTW, if you want to navigate without reloading, why not useRouter
?
– SiddAjmera
Nov 23 '18 at 18:31
I don't know how to do that ???
– drtapha
Nov 23 '18 at 18:38
What exactly are you trying to achieve here? And also, what exactly do you mean by When i navigate back, the history is not good?
– SiddAjmera
Nov 23 '18 at 18:39
I have a route videos and I want when I click on a video, the id of the video is displayed on the url like videos/id without reloading page. I use that id to show an overlay and display the good video
– drtapha
Nov 23 '18 at 18:43
I've added an answer. Check it to see if it helps.
– SiddAjmera
Nov 23 '18 at 19:12
add a comment |
1
Check ifLocation
is what you're looking for. BTW, if you want to navigate without reloading, why not useRouter
?
– SiddAjmera
Nov 23 '18 at 18:31
I don't know how to do that ???
– drtapha
Nov 23 '18 at 18:38
What exactly are you trying to achieve here? And also, what exactly do you mean by When i navigate back, the history is not good?
– SiddAjmera
Nov 23 '18 at 18:39
I have a route videos and I want when I click on a video, the id of the video is displayed on the url like videos/id without reloading page. I use that id to show an overlay and display the good video
– drtapha
Nov 23 '18 at 18:43
I've added an answer. Check it to see if it helps.
– SiddAjmera
Nov 23 '18 at 19:12
1
1
Check if
Location
is what you're looking for. BTW, if you want to navigate without reloading, why not use Router
?– SiddAjmera
Nov 23 '18 at 18:31
Check if
Location
is what you're looking for. BTW, if you want to navigate without reloading, why not use Router
?– SiddAjmera
Nov 23 '18 at 18:31
I don't know how to do that ???
– drtapha
Nov 23 '18 at 18:38
I don't know how to do that ???
– drtapha
Nov 23 '18 at 18:38
What exactly are you trying to achieve here? And also, what exactly do you mean by When i navigate back, the history is not good?
– SiddAjmera
Nov 23 '18 at 18:39
What exactly are you trying to achieve here? And also, what exactly do you mean by When i navigate back, the history is not good?
– SiddAjmera
Nov 23 '18 at 18:39
I have a route videos and I want when I click on a video, the id of the video is displayed on the url like videos/id without reloading page. I use that id to show an overlay and display the good video
– drtapha
Nov 23 '18 at 18:43
I have a route videos and I want when I click on a video, the id of the video is displayed on the url like videos/id without reloading page. I use that id to show an overlay and display the good video
– drtapha
Nov 23 '18 at 18:43
I've added an answer. Check it to see if it helps.
– SiddAjmera
Nov 23 '18 at 19:12
I've added an answer. Check it to see if it helps.
– SiddAjmera
Nov 23 '18 at 19:12
add a comment |
1 Answer
1
active
oldest
votes
You can follow these steps to achieve it:
import { Routes, RouterModule } from '@angular/router';
- Create a routes config:
const routes: Routes = [
{ path: '', redirectTo: '/videos', pathMatch: 'full' },
{ path: 'videos', component: VideosComponent, children: [
{ path: ':id', component: VideoComponent },
{ path: '**', component: PlaceholderComponent }
] },
];
- Add
RouterModule.forRoot(routes)
to theimports
array of your@NgModule
:
@NgModule({
imports: [..., RouterModule.forRoot(routes), ...],
...
})
export class AppModule { }
- Add a
router-outlet
tag to yourapp.component.html
along with an anchor tag tovideos
route:
<a routerLink="/videos">
Videos
</a>
<router-outlet></router-outlet>
- In videos.component.html create another
router-outlet
tag with links to videos:
...
<div>
<a routerLink="./1">Video 1</a> |
<a routerLink="./2">Video 2</a> |
<a routerLink="./3">Video 3</a> |
<a routerLink="./4">Video 4</a> |
<a routerLink="./5">Video 5</a> |
</div>
<br>
<router-outlet></router-outlet>
- You can now get the Video Id in the video component using
ActivatedRoute
. Use it to display the appropriate video:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-video',
templateUrl: './video.component.html',
styleUrls: ['./video.component.css']
})
export class VideoComponent implements OnInit {
videoId;
constructor(private activatedRoute: ActivatedRoute) { }
ngOnInit() {
this.activatedRoute.params.subscribe(params => this.videoId = params['id']);
}
}
Here's a Sample StackBlitz for your ref.
UPDATE:
If you don't want to load all the videos, then don't make :id
route as a child route to videos
. Make this change to your routes config:
const routes: Routes = [
{ path: 'videos', component: VideosComponent },
{ path: 'videos/:id', component: VideoComponent },
{ path: '', redirectTo: '/videos', pathMatch: 'full' }
];
And get rid of the additional router-outlet
from the VideosComponent
. That should be as per the way you want it. I've updated the Sample StackBlitz as well.
UPDATE 2
If you want to display some sort of a modal, then you can use ngx-bootstrap
. And then in the video component's ngOnInit
open the modal. When the user clicks to close the modal, just navigate back to /videos
route.
import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { LocationListenerService } from '../location-listener.service';
@Component({
selector: 'app-video',
templateUrl: './video.component.html',
styleUrls: ['./video.component.css']
})
export class VideoComponent implements OnInit {
videoId;
modalRef: BsModalRef;
@ViewChild('template') template: TemplateRef<any>;
constructor(
private activatedRoute: ActivatedRoute,
private modalService: BsModalService,
private router: Router
) { }
ngOnInit() {
this.activatedRoute.params.subscribe(params => this.videoId = params['id']);
if(!this.modalRef) {
this.openModal(this.template);
}
}
openModal(template: TemplateRef<any>) {
this.modalRef = this.modalService.show(template);
}
closeModal() {
this.modalRef.hide();
this.router.navigate(['/videos']);
}
}
And in your template:
<ng-template #template>
<div class="modal-header">
<h4 class="modal-title pull-left">Modal</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="closeModal()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>
Playing Video {{ videoId }}
</p>
</div>
</ng-template>
This should do the job for you. I've re-edited the Sample StackBlitz. Just have a look.
PS: It won't navigate back to /videos
on overlay click but the modal will close. That's just one case which you can take care of on your own. Rest all I've explained in the answer. Hope this helps.
UPDATE 3:
For showing the videos in the background, you'll have to again make the ':id'
path, a child of the videos
path. But then that would again require you to get all the videos to show upfront which is a condition that you wanted to avoid.
I've anyway made an update to the StackBlitz considering what you recently asked me to achieve.
thanks you but it's a good approch. but i navigate directly to /videos/id. I'll have to load all the videos and that's what I do not want to do
– drtapha
Nov 23 '18 at 19:18
exactly keeping the same url
– drtapha
Nov 23 '18 at 19:22
I want if the user is on the videos page and clicks on a video; I open an overlay to display the video by changing the url like /videos/id and if it does not, if it directly videos/id open another page
– drtapha
Nov 23 '18 at 19:31
i have concret example, i want something like instagram.com/explore
– drtapha
Nov 23 '18 at 19:33
thank you for your help, I just needed that
– drtapha
Nov 23 '18 at 21:46
|
show 3 more comments
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%2f53451480%2fhow-to-use-pushstate-on-angular%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
You can follow these steps to achieve it:
import { Routes, RouterModule } from '@angular/router';
- Create a routes config:
const routes: Routes = [
{ path: '', redirectTo: '/videos', pathMatch: 'full' },
{ path: 'videos', component: VideosComponent, children: [
{ path: ':id', component: VideoComponent },
{ path: '**', component: PlaceholderComponent }
] },
];
- Add
RouterModule.forRoot(routes)
to theimports
array of your@NgModule
:
@NgModule({
imports: [..., RouterModule.forRoot(routes), ...],
...
})
export class AppModule { }
- Add a
router-outlet
tag to yourapp.component.html
along with an anchor tag tovideos
route:
<a routerLink="/videos">
Videos
</a>
<router-outlet></router-outlet>
- In videos.component.html create another
router-outlet
tag with links to videos:
...
<div>
<a routerLink="./1">Video 1</a> |
<a routerLink="./2">Video 2</a> |
<a routerLink="./3">Video 3</a> |
<a routerLink="./4">Video 4</a> |
<a routerLink="./5">Video 5</a> |
</div>
<br>
<router-outlet></router-outlet>
- You can now get the Video Id in the video component using
ActivatedRoute
. Use it to display the appropriate video:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-video',
templateUrl: './video.component.html',
styleUrls: ['./video.component.css']
})
export class VideoComponent implements OnInit {
videoId;
constructor(private activatedRoute: ActivatedRoute) { }
ngOnInit() {
this.activatedRoute.params.subscribe(params => this.videoId = params['id']);
}
}
Here's a Sample StackBlitz for your ref.
UPDATE:
If you don't want to load all the videos, then don't make :id
route as a child route to videos
. Make this change to your routes config:
const routes: Routes = [
{ path: 'videos', component: VideosComponent },
{ path: 'videos/:id', component: VideoComponent },
{ path: '', redirectTo: '/videos', pathMatch: 'full' }
];
And get rid of the additional router-outlet
from the VideosComponent
. That should be as per the way you want it. I've updated the Sample StackBlitz as well.
UPDATE 2
If you want to display some sort of a modal, then you can use ngx-bootstrap
. And then in the video component's ngOnInit
open the modal. When the user clicks to close the modal, just navigate back to /videos
route.
import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { LocationListenerService } from '../location-listener.service';
@Component({
selector: 'app-video',
templateUrl: './video.component.html',
styleUrls: ['./video.component.css']
})
export class VideoComponent implements OnInit {
videoId;
modalRef: BsModalRef;
@ViewChild('template') template: TemplateRef<any>;
constructor(
private activatedRoute: ActivatedRoute,
private modalService: BsModalService,
private router: Router
) { }
ngOnInit() {
this.activatedRoute.params.subscribe(params => this.videoId = params['id']);
if(!this.modalRef) {
this.openModal(this.template);
}
}
openModal(template: TemplateRef<any>) {
this.modalRef = this.modalService.show(template);
}
closeModal() {
this.modalRef.hide();
this.router.navigate(['/videos']);
}
}
And in your template:
<ng-template #template>
<div class="modal-header">
<h4 class="modal-title pull-left">Modal</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="closeModal()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>
Playing Video {{ videoId }}
</p>
</div>
</ng-template>
This should do the job for you. I've re-edited the Sample StackBlitz. Just have a look.
PS: It won't navigate back to /videos
on overlay click but the modal will close. That's just one case which you can take care of on your own. Rest all I've explained in the answer. Hope this helps.
UPDATE 3:
For showing the videos in the background, you'll have to again make the ':id'
path, a child of the videos
path. But then that would again require you to get all the videos to show upfront which is a condition that you wanted to avoid.
I've anyway made an update to the StackBlitz considering what you recently asked me to achieve.
thanks you but it's a good approch. but i navigate directly to /videos/id. I'll have to load all the videos and that's what I do not want to do
– drtapha
Nov 23 '18 at 19:18
exactly keeping the same url
– drtapha
Nov 23 '18 at 19:22
I want if the user is on the videos page and clicks on a video; I open an overlay to display the video by changing the url like /videos/id and if it does not, if it directly videos/id open another page
– drtapha
Nov 23 '18 at 19:31
i have concret example, i want something like instagram.com/explore
– drtapha
Nov 23 '18 at 19:33
thank you for your help, I just needed that
– drtapha
Nov 23 '18 at 21:46
|
show 3 more comments
You can follow these steps to achieve it:
import { Routes, RouterModule } from '@angular/router';
- Create a routes config:
const routes: Routes = [
{ path: '', redirectTo: '/videos', pathMatch: 'full' },
{ path: 'videos', component: VideosComponent, children: [
{ path: ':id', component: VideoComponent },
{ path: '**', component: PlaceholderComponent }
] },
];
- Add
RouterModule.forRoot(routes)
to theimports
array of your@NgModule
:
@NgModule({
imports: [..., RouterModule.forRoot(routes), ...],
...
})
export class AppModule { }
- Add a
router-outlet
tag to yourapp.component.html
along with an anchor tag tovideos
route:
<a routerLink="/videos">
Videos
</a>
<router-outlet></router-outlet>
- In videos.component.html create another
router-outlet
tag with links to videos:
...
<div>
<a routerLink="./1">Video 1</a> |
<a routerLink="./2">Video 2</a> |
<a routerLink="./3">Video 3</a> |
<a routerLink="./4">Video 4</a> |
<a routerLink="./5">Video 5</a> |
</div>
<br>
<router-outlet></router-outlet>
- You can now get the Video Id in the video component using
ActivatedRoute
. Use it to display the appropriate video:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-video',
templateUrl: './video.component.html',
styleUrls: ['./video.component.css']
})
export class VideoComponent implements OnInit {
videoId;
constructor(private activatedRoute: ActivatedRoute) { }
ngOnInit() {
this.activatedRoute.params.subscribe(params => this.videoId = params['id']);
}
}
Here's a Sample StackBlitz for your ref.
UPDATE:
If you don't want to load all the videos, then don't make :id
route as a child route to videos
. Make this change to your routes config:
const routes: Routes = [
{ path: 'videos', component: VideosComponent },
{ path: 'videos/:id', component: VideoComponent },
{ path: '', redirectTo: '/videos', pathMatch: 'full' }
];
And get rid of the additional router-outlet
from the VideosComponent
. That should be as per the way you want it. I've updated the Sample StackBlitz as well.
UPDATE 2
If you want to display some sort of a modal, then you can use ngx-bootstrap
. And then in the video component's ngOnInit
open the modal. When the user clicks to close the modal, just navigate back to /videos
route.
import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { LocationListenerService } from '../location-listener.service';
@Component({
selector: 'app-video',
templateUrl: './video.component.html',
styleUrls: ['./video.component.css']
})
export class VideoComponent implements OnInit {
videoId;
modalRef: BsModalRef;
@ViewChild('template') template: TemplateRef<any>;
constructor(
private activatedRoute: ActivatedRoute,
private modalService: BsModalService,
private router: Router
) { }
ngOnInit() {
this.activatedRoute.params.subscribe(params => this.videoId = params['id']);
if(!this.modalRef) {
this.openModal(this.template);
}
}
openModal(template: TemplateRef<any>) {
this.modalRef = this.modalService.show(template);
}
closeModal() {
this.modalRef.hide();
this.router.navigate(['/videos']);
}
}
And in your template:
<ng-template #template>
<div class="modal-header">
<h4 class="modal-title pull-left">Modal</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="closeModal()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>
Playing Video {{ videoId }}
</p>
</div>
</ng-template>
This should do the job for you. I've re-edited the Sample StackBlitz. Just have a look.
PS: It won't navigate back to /videos
on overlay click but the modal will close. That's just one case which you can take care of on your own. Rest all I've explained in the answer. Hope this helps.
UPDATE 3:
For showing the videos in the background, you'll have to again make the ':id'
path, a child of the videos
path. But then that would again require you to get all the videos to show upfront which is a condition that you wanted to avoid.
I've anyway made an update to the StackBlitz considering what you recently asked me to achieve.
thanks you but it's a good approch. but i navigate directly to /videos/id. I'll have to load all the videos and that's what I do not want to do
– drtapha
Nov 23 '18 at 19:18
exactly keeping the same url
– drtapha
Nov 23 '18 at 19:22
I want if the user is on the videos page and clicks on a video; I open an overlay to display the video by changing the url like /videos/id and if it does not, if it directly videos/id open another page
– drtapha
Nov 23 '18 at 19:31
i have concret example, i want something like instagram.com/explore
– drtapha
Nov 23 '18 at 19:33
thank you for your help, I just needed that
– drtapha
Nov 23 '18 at 21:46
|
show 3 more comments
You can follow these steps to achieve it:
import { Routes, RouterModule } from '@angular/router';
- Create a routes config:
const routes: Routes = [
{ path: '', redirectTo: '/videos', pathMatch: 'full' },
{ path: 'videos', component: VideosComponent, children: [
{ path: ':id', component: VideoComponent },
{ path: '**', component: PlaceholderComponent }
] },
];
- Add
RouterModule.forRoot(routes)
to theimports
array of your@NgModule
:
@NgModule({
imports: [..., RouterModule.forRoot(routes), ...],
...
})
export class AppModule { }
- Add a
router-outlet
tag to yourapp.component.html
along with an anchor tag tovideos
route:
<a routerLink="/videos">
Videos
</a>
<router-outlet></router-outlet>
- In videos.component.html create another
router-outlet
tag with links to videos:
...
<div>
<a routerLink="./1">Video 1</a> |
<a routerLink="./2">Video 2</a> |
<a routerLink="./3">Video 3</a> |
<a routerLink="./4">Video 4</a> |
<a routerLink="./5">Video 5</a> |
</div>
<br>
<router-outlet></router-outlet>
- You can now get the Video Id in the video component using
ActivatedRoute
. Use it to display the appropriate video:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-video',
templateUrl: './video.component.html',
styleUrls: ['./video.component.css']
})
export class VideoComponent implements OnInit {
videoId;
constructor(private activatedRoute: ActivatedRoute) { }
ngOnInit() {
this.activatedRoute.params.subscribe(params => this.videoId = params['id']);
}
}
Here's a Sample StackBlitz for your ref.
UPDATE:
If you don't want to load all the videos, then don't make :id
route as a child route to videos
. Make this change to your routes config:
const routes: Routes = [
{ path: 'videos', component: VideosComponent },
{ path: 'videos/:id', component: VideoComponent },
{ path: '', redirectTo: '/videos', pathMatch: 'full' }
];
And get rid of the additional router-outlet
from the VideosComponent
. That should be as per the way you want it. I've updated the Sample StackBlitz as well.
UPDATE 2
If you want to display some sort of a modal, then you can use ngx-bootstrap
. And then in the video component's ngOnInit
open the modal. When the user clicks to close the modal, just navigate back to /videos
route.
import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { LocationListenerService } from '../location-listener.service';
@Component({
selector: 'app-video',
templateUrl: './video.component.html',
styleUrls: ['./video.component.css']
})
export class VideoComponent implements OnInit {
videoId;
modalRef: BsModalRef;
@ViewChild('template') template: TemplateRef<any>;
constructor(
private activatedRoute: ActivatedRoute,
private modalService: BsModalService,
private router: Router
) { }
ngOnInit() {
this.activatedRoute.params.subscribe(params => this.videoId = params['id']);
if(!this.modalRef) {
this.openModal(this.template);
}
}
openModal(template: TemplateRef<any>) {
this.modalRef = this.modalService.show(template);
}
closeModal() {
this.modalRef.hide();
this.router.navigate(['/videos']);
}
}
And in your template:
<ng-template #template>
<div class="modal-header">
<h4 class="modal-title pull-left">Modal</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="closeModal()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>
Playing Video {{ videoId }}
</p>
</div>
</ng-template>
This should do the job for you. I've re-edited the Sample StackBlitz. Just have a look.
PS: It won't navigate back to /videos
on overlay click but the modal will close. That's just one case which you can take care of on your own. Rest all I've explained in the answer. Hope this helps.
UPDATE 3:
For showing the videos in the background, you'll have to again make the ':id'
path, a child of the videos
path. But then that would again require you to get all the videos to show upfront which is a condition that you wanted to avoid.
I've anyway made an update to the StackBlitz considering what you recently asked me to achieve.
You can follow these steps to achieve it:
import { Routes, RouterModule } from '@angular/router';
- Create a routes config:
const routes: Routes = [
{ path: '', redirectTo: '/videos', pathMatch: 'full' },
{ path: 'videos', component: VideosComponent, children: [
{ path: ':id', component: VideoComponent },
{ path: '**', component: PlaceholderComponent }
] },
];
- Add
RouterModule.forRoot(routes)
to theimports
array of your@NgModule
:
@NgModule({
imports: [..., RouterModule.forRoot(routes), ...],
...
})
export class AppModule { }
- Add a
router-outlet
tag to yourapp.component.html
along with an anchor tag tovideos
route:
<a routerLink="/videos">
Videos
</a>
<router-outlet></router-outlet>
- In videos.component.html create another
router-outlet
tag with links to videos:
...
<div>
<a routerLink="./1">Video 1</a> |
<a routerLink="./2">Video 2</a> |
<a routerLink="./3">Video 3</a> |
<a routerLink="./4">Video 4</a> |
<a routerLink="./5">Video 5</a> |
</div>
<br>
<router-outlet></router-outlet>
- You can now get the Video Id in the video component using
ActivatedRoute
. Use it to display the appropriate video:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-video',
templateUrl: './video.component.html',
styleUrls: ['./video.component.css']
})
export class VideoComponent implements OnInit {
videoId;
constructor(private activatedRoute: ActivatedRoute) { }
ngOnInit() {
this.activatedRoute.params.subscribe(params => this.videoId = params['id']);
}
}
Here's a Sample StackBlitz for your ref.
UPDATE:
If you don't want to load all the videos, then don't make :id
route as a child route to videos
. Make this change to your routes config:
const routes: Routes = [
{ path: 'videos', component: VideosComponent },
{ path: 'videos/:id', component: VideoComponent },
{ path: '', redirectTo: '/videos', pathMatch: 'full' }
];
And get rid of the additional router-outlet
from the VideosComponent
. That should be as per the way you want it. I've updated the Sample StackBlitz as well.
UPDATE 2
If you want to display some sort of a modal, then you can use ngx-bootstrap
. And then in the video component's ngOnInit
open the modal. When the user clicks to close the modal, just navigate back to /videos
route.
import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { LocationListenerService } from '../location-listener.service';
@Component({
selector: 'app-video',
templateUrl: './video.component.html',
styleUrls: ['./video.component.css']
})
export class VideoComponent implements OnInit {
videoId;
modalRef: BsModalRef;
@ViewChild('template') template: TemplateRef<any>;
constructor(
private activatedRoute: ActivatedRoute,
private modalService: BsModalService,
private router: Router
) { }
ngOnInit() {
this.activatedRoute.params.subscribe(params => this.videoId = params['id']);
if(!this.modalRef) {
this.openModal(this.template);
}
}
openModal(template: TemplateRef<any>) {
this.modalRef = this.modalService.show(template);
}
closeModal() {
this.modalRef.hide();
this.router.navigate(['/videos']);
}
}
And in your template:
<ng-template #template>
<div class="modal-header">
<h4 class="modal-title pull-left">Modal</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="closeModal()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>
Playing Video {{ videoId }}
</p>
</div>
</ng-template>
This should do the job for you. I've re-edited the Sample StackBlitz. Just have a look.
PS: It won't navigate back to /videos
on overlay click but the modal will close. That's just one case which you can take care of on your own. Rest all I've explained in the answer. Hope this helps.
UPDATE 3:
For showing the videos in the background, you'll have to again make the ':id'
path, a child of the videos
path. But then that would again require you to get all the videos to show upfront which is a condition that you wanted to avoid.
I've anyway made an update to the StackBlitz considering what you recently asked me to achieve.
edited Nov 24 '18 at 6:45
answered Nov 23 '18 at 19:07
SiddAjmeraSiddAjmera
14.5k31137
14.5k31137
thanks you but it's a good approch. but i navigate directly to /videos/id. I'll have to load all the videos and that's what I do not want to do
– drtapha
Nov 23 '18 at 19:18
exactly keeping the same url
– drtapha
Nov 23 '18 at 19:22
I want if the user is on the videos page and clicks on a video; I open an overlay to display the video by changing the url like /videos/id and if it does not, if it directly videos/id open another page
– drtapha
Nov 23 '18 at 19:31
i have concret example, i want something like instagram.com/explore
– drtapha
Nov 23 '18 at 19:33
thank you for your help, I just needed that
– drtapha
Nov 23 '18 at 21:46
|
show 3 more comments
thanks you but it's a good approch. but i navigate directly to /videos/id. I'll have to load all the videos and that's what I do not want to do
– drtapha
Nov 23 '18 at 19:18
exactly keeping the same url
– drtapha
Nov 23 '18 at 19:22
I want if the user is on the videos page and clicks on a video; I open an overlay to display the video by changing the url like /videos/id and if it does not, if it directly videos/id open another page
– drtapha
Nov 23 '18 at 19:31
i have concret example, i want something like instagram.com/explore
– drtapha
Nov 23 '18 at 19:33
thank you for your help, I just needed that
– drtapha
Nov 23 '18 at 21:46
thanks you but it's a good approch. but i navigate directly to /videos/id. I'll have to load all the videos and that's what I do not want to do
– drtapha
Nov 23 '18 at 19:18
thanks you but it's a good approch. but i navigate directly to /videos/id. I'll have to load all the videos and that's what I do not want to do
– drtapha
Nov 23 '18 at 19:18
exactly keeping the same url
– drtapha
Nov 23 '18 at 19:22
exactly keeping the same url
– drtapha
Nov 23 '18 at 19:22
I want if the user is on the videos page and clicks on a video; I open an overlay to display the video by changing the url like /videos/id and if it does not, if it directly videos/id open another page
– drtapha
Nov 23 '18 at 19:31
I want if the user is on the videos page and clicks on a video; I open an overlay to display the video by changing the url like /videos/id and if it does not, if it directly videos/id open another page
– drtapha
Nov 23 '18 at 19:31
i have concret example, i want something like instagram.com/explore
– drtapha
Nov 23 '18 at 19:33
i have concret example, i want something like instagram.com/explore
– drtapha
Nov 23 '18 at 19:33
thank you for your help, I just needed that
– drtapha
Nov 23 '18 at 21:46
thank you for your help, I just needed that
– drtapha
Nov 23 '18 at 21:46
|
show 3 more comments
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.
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%2f53451480%2fhow-to-use-pushstate-on-angular%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
1
Check if
Location
is what you're looking for. BTW, if you want to navigate without reloading, why not useRouter
?– SiddAjmera
Nov 23 '18 at 18:31
I don't know how to do that ???
– drtapha
Nov 23 '18 at 18:38
What exactly are you trying to achieve here? And also, what exactly do you mean by When i navigate back, the history is not good?
– SiddAjmera
Nov 23 '18 at 18:39
I have a route videos and I want when I click on a video, the id of the video is displayed on the url like videos/id without reloading page. I use that id to show an overlay and display the good video
– drtapha
Nov 23 '18 at 18:43
I've added an answer. Check it to see if it helps.
– SiddAjmera
Nov 23 '18 at 19:12