Dhrupal Suthar
Dhrupal Suthar

Reputation: 329

How to redirect Page in Angular?

I'm Trying to redirect one component to another (like page redirect)

Angular link

here is my code

app-routing.module.ts

import { RoleComponent } from './role/role.component';
import { AppComponent } from './app.component';
import { NgModule }             from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  { path: '', redirectTo: '/AppComponent', pathMatch: 'full' },
  { path: 'role', component: RoleComponent },

];

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {}

top-header.component.html

 <ul class="navbar-nav mr-auto">
       <li class="nav-item">
          <a class="nav-link" href="#">Verified</a>
        </li>
        <li class="nav-item">
          <a routerLink="/role" class="nav-link" href="#">ROLES</a>
        </li>
      </ul>

AppComponent.html

<div class="container p-md-0">
<app-top-header></app-top-header>
<router-outlet></router-outlet>
<app-main-contain></app-main-contain>
</div>

Upvotes: 32

Views: 155910

Answers (6)

Deepshikha Chaudhary
Deepshikha Chaudhary

Reputation: 398

You can use router.navigate function of the Router class. Just import Router from @angular/router and create a object for it in the constructor(private router: Router) and use router.navigate(['/roles']);

import { RoleComponent } from './role/role.component';
import { AppComponent } from './app.component';
import { NgModule }             from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  { path: '', redirectTo: '/AppComponent', pathMatch: 'full' },
  { path: 'role', component: RoleComponent },

];

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {
  constructor(private router: Router)
}

functionOnWhichRedirectShouldHappen(){
  this.router.navigate(['/role']);
}

HTML

<ul class="navbar-nav mr-auto">
   <li class="nav-item">
       <a class="nav-link" href="#">Verified</a>
   </li>
   <li class="nav-item">
      <button (click)="functionOnWhichRedirectShouldHappen()">ROLES</button>
   </li>
</ul>

Upvotes: 31

bart-khalid
bart-khalid

Reputation: 348

You can simply redirect your page to another one by adding this to your app-routing.module.ts

file :

{ path: '', redirectTo: 'component route name', pathMatch: 'full' },
{ path: 'component route name', component: MyComponent },

Upvotes: 0

Farouk Mhamdi
Farouk Mhamdi

Reputation: 321

I used this solution :

  redirectTo() {
    window.history.back()
  }

Upvotes: 1

manjeet chauhan
manjeet chauhan

Reputation: 11

just add this path in your app-routing.module.ts file so that it can know the path:

{ path: 'AppComponent', component: AppComponent }

Upvotes: 1

Darkade
Darkade

Reputation: 947

Your app-routing.module.ts is fine. Now, in your component you can do Import Router and use it to redirect.

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

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  constructor(private router: Router) { }

  ngOnInit() { }

  redirect() {
      this.router.navigate(['role']);
  }

}

And in your component.html just call that redirect() function

<button (click)="redirect()"></button>

Upvotes: 23

PPL
PPL

Reputation: 6565

Try below code

You can use Angular $window

$window.location.href = '/index.html';

Example usage in a contoller:

(function () {
    'use strict';

    angular
        .module('app')
        .controller('LoginCtrl', LoginCtrl);

    LoginCtrl.$inject = ['$window', 'loginSrv', 'notify'];

    function LoginCtrl($window, loginSrv, notify) {
        /* jshint validthis:true */
        var vm = this;
        vm.validateUser = function () {
             loginSrv.validateLogin(vm.username, vm.password).then(function (data) {          
                if (data.isValidUser) {    
                    $window.location.href = '/index.html';
                }
                else
                    alert('Login incorrect');
            });
        }
    }
})();

Upvotes: -3

Related Questions