Angular 4 router and modal dialog

Sergey picture Sergey · Jun 16, 2017 · Viewed 18.2k times · Source

I have Angular 4 SPA application using Angular router. I want to have hyperlink that opens a component in new dialog using Bootstrap 4. I already know how to open modal dialog from a function.

But how to open it using hyperlink?

<a [routerLink]="['/login']">Login</a>

I want to leave my current component in and just show modal dialog in front of it.

Another question - is it possible to do that programatically? So that I can

this.router.navigate(['/login']);

and login modal dialog is displayed over current component?

Any suggestions?

Answer

birwin picture birwin · Jun 16, 2017

My best guess is you may want to subscribe to the activated route and change params in the route to trigger the modal.

import { ActivatedRoute, Params } from '@angular/router';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'cmp1',
  templateUrl: './cmp1.component.html',
  styleUrls: ['./cmp1.component.css'],
})
export class Cmp1 implements OnInit {

    constructor(private activatedRoute: ActivatedRoute) {
    }

    ngOnInit() {
        this.activatedRoute.params.subscribe(params => {
            if (params["modal"] == 'true') {
                // Launch Modal here
            }
        });
    }
}

I believe you would then have a link that looked something like this: <a [routerLink]="['/yourroute', {modal: 'true'}]">

Better examples might be found here: Route Blog