String interpolation in Typescript, replacing ‘placeholders’ with variables

Use a template string which are much better than String.Format in my opinion as they do not suffer from poor indexing (wrong placeholder) issues: var text = “blah blah”; var strTest = `This is a ${text}`; console.log(strTest); If I do not know the name of the variables I need to pass in?? Then wrap in … Read more

How to get one file as output of angular cli

Angular CLI natively doesn’t support this. There are other solutions, including further processing with other tooling after Angular CLI finishes doing its thing, but this will hinder or remove the debugging capabilities Angular provides out-of-the-box. As the ng eject command is also being deprecated, the option to reconfigure webpack is not as attractive as it … Read more

.detectChanges() not working within Angular test

It turns out this is due to using ChangeDetectionStrategy.OnPush in the component. Using OnPush only allows you to call .detectChanges() one time, so subsequent calls will fail to do anything. I’m not familiar enough with Angular to fully understand why. I was able to produce the required behaviour by overriding the ChangeDetectionStrategy in my TestBed … Read more

How do I add svg files via MatIconRegistry in unit tests?

can just do: import { MatIcon } from ‘@angular/material/icon’; import { MatIconTestingModule } from ‘@angular/material/icon/testing’; describe(‘MyComponent’, () => { beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [MyComponent, MatIcon], imports: [MatIconTestingModule], }).compileComponents(); })); … }); This will generate a test icon rendering without the HTTP request. NOTE: Newer versions of Angular, e.g., >16, may produce an error unless … Read more

Different routes same component

you can solve it by adding routes const routes: Routes = [ { path: ‘experience’, children: [ { path: ‘pending’, component: ExperienceComponent }, { path: ‘requests’, component: ExperienceComponent }, ] }] and in ExperienceComponent import import { ActivatedRoute } from “@angular/router”; and in constructor add this parameter constructor(public route: ActivatedRoute) and inside constructor get url … Read more

Angular: How to correctly implement APP_INITIALIZER

Due to how APP_INTIALIZER works, it’s expected that asynchronous initializers return promises, but your implementation of APP_INTIALIZER multiprovider doesn’t because loadConfigurationData function doesn’t return anything. It should be something like: loadConfigurationData(): Promise<Configuration> { return this.http.get<Configuration>(`${this.originUrl}${this.configUrlPath}`) .do(result => { this.configData = result; }) .toPromise(); }

Replace deprecated Angular ComponentFactoryResolver, ComponentFactory

In Angular 13 the new API removes the need for ComponentFactoryResolver being injected into the constructor, like you did in your code. Now to dynamically create a component you have to use ViewContainerRef.createComponent without using the factory. So, instead of using const compFactory: ComponentFactory<any> = this.resolver.resolveComponentFactory(DynamicTableComponent); you can do: import {ViewContainerRef} from ‘@angular/core’; /** your … Read more

Is it necessary to unsubscribe from observables created by Http methods to avoid memory leaks?

So the answer is no, you don’t. Ng2 will clean it up itself. The Http service source, from Angular’s Http XHR backend source: Notice how it runs the complete() after getting the result. This means it actually unsubscribes on completion. So you don’t need to do it yourself. Here is a test to validate: fetchFilms() … Read more