Angular 6 add items into Observable

If this.contacts is an Observable of list of objects (contacts: Observable<Items[]>) and you want to make some changes to that list, you can simply use tap: import { tap } from ‘rxjs/operators’; this.contacts.pipe(tap(usersList => { usersList.push(newItem); })); But if you want to make another request to the server and merge these lists, you can use … Read more

How to conditionally inject service into component?

You can use the Injector import { Injector } from ‘@angular/core’ … constructor(private injector: Injector){ if(true) { this.oneService = <OneService>this.injector.get(OneService); } else { this.twoService = <TwoService>this.injector.get(TwoService); } } As @MeirionHughes mentioned this is called the service locator pattern: The technique is an example of the service locator pattern. Avoid this technique unless you genuinely need … Read more

Karma unit testing error: Unexpected value imported by the module. Please add a @NgModule annotation

You are injecting MatDialogRef in component: constructor(private dialogRef: MatDialogRef<Mytest1Component>) { } So the testBed expects the same to be injected as provider to the TestBed. Or you can also provide a MockDialogueService to it. beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ Mytest1Component ], providers: [ MatDialogRef ], }) .compileComponents(); }));

Filter array of objects by array of ids

You can simply use array.filter with indexOf to check the matching element in the next array. var arr = serviceList.filter(item => activeIds.indexOf(item.id) === -1); DEMO let activeIds = [202, 204] let serviceList = [{ “id”:201, “title”:”a” }, { “id”:202, “title”:”a” }, { “id”:203, “title”:”c” }, { “id”:204, “title”:”d” }, { “id”:205, “title”:”e” }]; let arr … Read more

What is the difference between createEmbeddedView and createComponent?

See this workshop on DOM manipulation or read Working with DOM in Angular: unexpected consequences and optimization techniques where I explain the difference with examples. These both methods are used to dynamically add content to the component view (DOM). This content can be either a template or a component based. In Angular we usually manipulate … Read more

Refactor this method to reduce its Cognitive Complexity from 21 to the 15 allowed. How to refactor and reduce the complexity

A little information on how cognitive complexity works and why you should keep it low First of all it is important to understand how “Cognitive Complexity” works as compared to “Cyclomatic Complexity“. Cognitive complexity takes into account the complexity perceived by the human brain. This is why it does not simply indicate the number of … Read more

Build and use npm package locally

If you have made these changes on your machine. (I’m assuming you have) Run a build of the ngx-mask package that you changed. run npm pack from that package’s root folder. This creates a .tgz zip file of your package with your custom modifications. copy that file into the root (you could put it wherever … Read more

Is there a way to typehint IDE for “let variable” type in Typescript and Angular?

as the tableDataSource: MatTableDataSource<ToDoInterface>; does not type the model, this: <ng-container matColumnDef=”toDo”> <th mat-header-cell *matHeaderCellDef mat-sort-header>ToDo</th> <td mat-cell *matCellDef=”let model”> <ng-container *ngIf=”assertItemType(model) as toDoModel”> {{toDoModel.toDo}} </ng-container> </td> </ng-container> where: assertItemType(item: ToDoInterface): ToDoInterface { return item; } works. but not sure if it the best way to do it