Why must must ngrx / redux effects return actions? Is using a noop action like elm considered bad practice?

By default, an ngrx/effect dispatches an action. If you want an effect to be ‘fire-and-forget’, all you need to do is add {dispatch: false} as an argument to the @Effects() decorator. From the @ngrx/effects docs: Observables decorated with the @Effect() decorator are expected to be a stream of actions to be dispatched. Pass { dispatch: … Read more

Angular router url returns slash

Instead of trying to use Router (which is not ready to give you final route at this moment of navigation lifecycle), you can use Location service (https://angular.io/api/common/Location) and its method path, which will give you the url without base href. On the contrary, window.location.pathname is not aware of your angular toys and will give you … Read more

Angular2 Get reference to element created in ngFor

If you want to add/remove a class use binding <div *ngFor=”let cookie of cookies” [class.isActive]=”someExpression” ….> or <div *ngFor=”let cookie of cookies” [ngStyle]=”{‘isActive’: someExpression}” ….> Concrete example: activeCookie:string = ‘b’; cookies:string[] = [‘a’, ‘b’, ‘c’]; <div *ngFor=”letcookie of cookies” [class.isActive]=”cookie == activeCookie” ….> If you really want to get a reference you can use <div … Read more

Angular2 Detect if element in template view has class

Add a template variable to be able to query the element. <div #patientDDL class=”overlay-dropdown dropdown” id=”patientDDL”> Query the element @ViewChild(‘patientDDL’) patientDDL:ElementRef; Implement ngDoCheck() to run the check whether the class was added when change detection runs: ngDoCheck() { if(patientDDL.nativeElement.classList.contains(‘open’)) { this.doSomething(); } } or on some specific event @HostListener(‘click’, [‘$event’]) clickHandler(event) { if(patientDDL.nativeElement.classList.contains(‘open’)) { this.doSomething(); … Read more

Angular pass resolve data to child-routes

You have two options here: 1. You can access parent resolve data via the child’s resolver by creating a child-specific resolver and accessing the route’s parent property. […module.ts | …component.ts] { path: ‘project/:projectId’, component: ProjectDetailComponent, resolve: { project: ProjectResolver } children: [ { path: ‘:projectId/edit’, component: EditProjectComponent, resolve: { edit: EditProjectResolve } } ] } … Read more

Angular: AfterViewInit() vs AfterContentInit()

This starts to make more sense if you’ve began some dynamic component loading or manual view creation in Angular. OnInit is called once when the component’s @Inputs and @Outputs have been resolved. AfterViewInit is called when the component’s view has been attached. Remember that Angular compiles all views to JS files, not html – the … Read more