Home > Software engineering >  How to navigate to 2 url's on a button click in Angular
How to navigate to 2 url's on a button click in Angular

Time:09-07

When user clicks the button they should be navigated to www.example.com/logout and then automatically redirected to an iframe (www.example.com/home)

        setTimeout(window.location.href = "https://example.com/logout", 2000);
        this.iframeUrl = "https://example.com/home";

CodePudding user response:

<button (click)="OnClick()"></button>
public onClick(){
 this.http.post('www.example.com/logout').subsribe(data =>{
    //on success redirect to home
    },err => {
    //on error keep it in same page
    })
}

this is how we have to do the call on click of button

hopefully this helps you

CodePudding user response:

Here is a demo of how we can use routing after a interval of 3 seconds, demo happens when you click the logout link of home!

logout

import { Component, OnDestroy, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
  selector: 'app-logout',
  templateUrl: './logout.component.html',
  styleUrls: ['./logout.component.css'],
})
export class LogoutComponent implements OnInit, OnDestroy {
  timeout: any;
  constructor(private router: Router) {}

  ngOnInit() {
    this.timeout = setTimeout(() => {
      this.router.navigate(['home']);
    }, 3000);
  }

  ngOnDestroy() {
    clearTimeout(this.timeout);
  }
}

forked stackblitz

  • Related