Cheatography
https://cheatography.com
Router asIs toBe in angular hybrid application.
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Routing in Angular Hybrid Apllication
Hybrid Application: AngularJs (1.3.7) + Angular (12)
Router:
UI-Router
is used in AngularJs and @angular/router
is used in Angular.
This document describe more about routing AS-IS in AngularJs and TO-BE in Angular. |
Imports
var appModule = angular.module('appModule', [ 'ui.router' ]);
|
import { RouterModule } from '@angular/router';
|
State Definition
$stateProvider.state("user-details", { url: "/user/:id", views: { "index@": { templateUrl: "user-details.html", controller: "controller as ctrl", }, }, });
|
RouterModule.forRoot([ { path: 'user/:id', component: UserDetailComponent }, ]);
|
View Creation
<ui-view name="viewName"></ui-view> |
<router-outlet name="viewName"></router-outlet> |
<div ui-view="viewName"></div> |
Params Access
Service: $stateParams |
Provider: ActivatedRoute |
|
Snapshot: Navigation: user/1 => users-listing => user/2 constructor(private route: ActivatedRoute) { } ngOnInit() { this.id = this.route.snapshot.params.id; }
|
|
Observable: Navigation: user/1 => user/2 => user/3 constructor(private route: ActivatedRoute) { } ngOnInit() { this.route.paramMap.subscribe(params: ParamMap => { this.id = params.get('id'); }); }
|
Navigation between AngularJs and Angular
NG1 => NG12 |
NG12 => NG1 |
using $location service |
|
$location.url('/user/2'); |
this.location.go('/user/2') |
|
|
|
router.navigateByUrl('/user/2') |
|
router.navigate(['user', user.id]) // user.id=2 |
|