Handle @Input and @Output for dynamically created Component in Angular 2

You can easily bind it when you create the component: createSub() { const factory = this.resolver.resolveComponentFactory(SubComponent); const ref = this.location.createComponent(factory, this.location.length, this.location.parentInjector, []); ref.someData = { data: ‘123’ }; // send data to input ref.onClick.subscribe( // subscribe to event emitter (event: any) => { console.log(‘click’); } ) ref.changeDetectorRef.detectChanges(); return ref; } Sending data is really … Read more

Error: Cannot match any routes. URL Segment: – Angular 2

Solved myself. Done some small structural changes also. Route from Component1 to Component2 is done by a single <router-outlet>. Component2 to Comonent3 and Component4 is done by multiple <router-outlet name= “xxxxx”> The resulting contents are : Component1.html <nav> <a routerLink=”/two” class=”dash-item”>Go to 2</a> </nav> <router-outlet></router-outlet> Component2.html <a [routerLink]=”[‘/two’, {outlets: {‘nameThree’: [‘three’]}}]”>In Two…Go to 3 … … Read more

Angular2: Make route paths case insensitive

Here’s what I did. import { DefaultUrlSerializer, UrlTree } from ‘@angular/router’; export class LowerCaseUrlSerializer extends DefaultUrlSerializer { parse(url: string): UrlTree { // Optional Step: Do some stuff with the url if needed. // If you lower it in the optional step // you don’t need to use “toLowerCase” // when you pass it down to … Read more

routerLink absolute url

As of August, 2018, Angular docs say: If the first segment begins with /, the router will look up the route from the root of the app. If the first segment begins with ./, or doesn’t begin with a slash, the router will instead look in the children of the current activated route. And if … Read more

Redirect to a different component inside @CanActivate in Angular2

As of today, with latest @angular/router 3.0.0-rc.1, here are a couple of references on how to do that through CanActivate guards on routes: angular 2 reference Two answers to this SO question, by Nilz11 and by Jason The main gist of logic looks like: // … canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { if (this.authService.isLoggedIn) { // … Read more