How to write unit testing for Angular / TypeScript for private methods with Jasmine

I’m with you, even though it’s a good goal to “only unit test the public API” there are times when it doesn’t seem that simple and you feel you are choosing between compromising either the API or the unit-tests. You know this already, since that’s exactly what you’re asking to do, so I won’t get … Read more

Angular2 Exception: Can’t bind to ‘routerLink’ since it isn’t a known native property

>=RC.5 import the RouterModule See also https://angular.io/guide/router @NgModule({ imports: [RouterModule], … }) >=RC.2 app.routes.ts import { provideRouter, RouterConfig } from ‘@angular/router’; export const routes: RouterConfig = [ … ]; export const APP_ROUTER_PROVIDERS = [provideRouter(routes)]; main.ts import { bootstrap } from ‘@angular/platform-browser-dynamic’; import { APP_ROUTER_PROVIDERS } from ‘./app.routes’; bootstrap(AppComponent, [APP_ROUTER_PROVIDERS]); <=RC.1 Your code is missing @Component({ … Read more

angular2 style guide – property with dollar sign?

$ suffix (popularized by Cycle.js) is used to indicate that the variable is an Observable. It could make it to the official style guide too but it’s not there yet Read more here : What does the suffixed dollar sign $ mean? Update: Read more about the trailing “$” sign on Angular website here: https://angular.io/guide/rx-library#naming-conventions-for-observables

Use component from another module

The main rule here is that: The selectors which are applicable during compilation of a component template are determined by the module that declares that component, and the transitive closure of the exports of that module’s imports. So, try to export it: @NgModule({ declarations: [TaskCardComponent], imports: [MdCardModule], exports: [TaskCardComponent] // <== export the component you … Read more

tslint / codelyzer / ng lint error: “for (… in …) statements must be filtered with an if statement”

To explain the actual problem that tslint is pointing out, a quote from the JavaScript documentation of the for…in statement: The loop will iterate over all enumerable properties of the object itself and those the object inherits from its constructor’s prototype (properties closer to the object in the prototype chain override prototypes’ properties). So, basically … Read more

How to pass query parameters with a routerLink

queryParams queryParams is another input of routerLink where they can be passed like <a [routerLink]=”[‘../’]” [queryParams]=”{prop: ‘xxx’}”>Somewhere</a> fragment <a [routerLink]=”[‘../’]” [queryParams]=”{prop: ‘xxx’}” [fragment]=”yyy”>Somewhere</a> routerLinkActiveOptions To also get routes active class set on parent routes: [routerLinkActiveOptions]=”{ exact: false }” To pass query parameters to this.router.navigate(…) use let navigationExtras: NavigationExtras = { queryParams: { ‘session_id’: sessionId }, … Read more

How can I write data attributes using Angular?

Use attribute binding syntax instead <ol class=”viewer-nav”><li *ngFor=”let section of sections” [attr.data-sectionvalue]=”section.value”>{{ section.text }}</li> </ol> or <ol class=”viewer-nav”><li *ngFor=”let section of sections” attr.data-sectionvalue=”{{section.value}}”>{{ section.text }}</li> </ol> See also : How to add conditional attribute in Angular 2?

What is the proper use of an EventEmitter?

TL;DR: No, don’t subscribe manually to them, don’t use them in services. Use them as is shown in the documentation only to emit events in components. Don’t defeat angular’s abstraction. Answer: No, you should not subscribe manually to it. EventEmitter is an angular2 abstraction and its only purpose is to emit events in components. Quoting … Read more

How to get query parameters from URL in Angular 5?

In Angular 5, the query params are accessed by subscribing to this.route.queryParams (note that later Angular versions recommend queryParamMap, see also other answers). Example: /app?param1=hallo&param2=123 param1: string; param2: string; constructor(private route: ActivatedRoute) { console.log(‘Called Constructor’); this.route.queryParams.subscribe(params => { this.param1 = params[‘param1’]; this.param2 = params[‘param2’]; }); } whereas, the path variables are accessed by this.route.snapshot.params Example: … Read more