How to test ‘private’ functions in an angular service with Karma and Jasmine

There is no way to test those functions. Their scope is the function that comprises your BracketService factory and they are invisible anyplace else. If you want to test them, then you have to expose them somehow. You can move them into their own service (which seems like overkill) or you can black box test … Read more

Invalid provider for the NgModule ‘DynamicTestModule’ when testing a service in Angular 2

This is such an annoying error, thought I’d include another subtle cause to look for in your spec. In my case I specified providers instead of the correct value of provide as below TestBed.configureTestingModule({ providers: [{provider: ApplicationActions, useClass: ActionMock}] rather than offer useful information like “no ‘provide’ key specified” it simply reports Failed: Invalid provider … Read more

difference between setTimeout in javascript and $timeout service in angularjs

There are some cases where one needs to perform some sort of timeout operation and we frequently achieve this using JavaScript’s setTimeout() function. However, if we use setTimeout() in an AngularJS application we also need to use $scope.$apply() to ensure that any changes to the scope will be reflected elsewhere (i.e. data-bound in a view). … Read more

Angular (v5) service is getting constructed before APP_INITIALIZER promise resolves

I had also a simmilar issue what solved the issue for me was to use Observable methods and operators to do everything. Then in the end just use the toPromise method of the Observable to return a Promise. This is also simpler because you don’t need to create a promise yourself. The AppConfig service will … Read more

Angular 9 : Error NG2003: No suitable injection token for parameter ‘url’ of class ‘DataService’. Found string

In DataService.ts : Update the Constructor. //import { Inject } from ‘@angular/core’; constructor(@Inject(String) private url: string, private http: Http) UPDATED (Explanation): According to https://angular-2-training-book.rangle.io/di/angular2/inject_and_injectable ; @Inject() is a manual mechanism for letting Angular know that a parameter must be injected. @Inject decorator is only needed for injecting primitives. The primitive types are number, string, boolean, … Read more

Correct way Provide DomSanitizer to Component with Angular 2 RC6

You don’t need to declare providers: [ DomSanitizer ] anymore. Just need to import DomSanitizer as shown below, import { DomSanitizer, SafeResourceUrl, SafeUrl} from ‘@angular/platform-browser’; in component inject it through a constructor as below, constructor(private sanitizer: DomSanitizer) {}

Get status code http.get response angular2

Adding answer for versions of Angular >= 4.3 (including 15) with new HttpClient that replaces http import {HttpClientModule} from ‘@angular/common/http’; // Notice it is imported from @angular/common/http instead of @angular/http How to get response code or any other header: http.get( `${this.baseUrl}users/activate?mailToken=${mailToken}`, {observe: ‘response’} ) .subscribe(response => { // You can access status: console.log(response.status); // Or … Read more

How to use angular2 built-in date pipe in services and directives script files [duplicate]

Since CommonModule does not export it as a provider you’ll have to do it yourself. This is not very complicated. 1) Import DatePipe: import { DatePipe } from ‘@angular/common’; 2) Include DatePipe in your module’s providers: NgModule({ providers: [DatePipe] }) export class AppModule { } or component’s providers: @Component({ selector: ‘home’, styleUrls: [‘./home.component.css’], templateUrl: ‘./home.component.html’, … Read more

AngularJS – UI Router – programmatically add states

See -edit- for updated information Normally states are added to the $stateProvider during the config phase. If you want to add states at runtime, you’ll need to keep a reference to the $stateProvider around. This code is untested, but should do what you want. It creates a service called runtimeStates. You can inject it into … Read more

tech