angular-dependency-injection

Angular dependency with “providedIn” in lazy loading modules

北城以北 提交于 2021-02-11 15:15:21
问题 I am using Angular "Lazy-loading feature modules" as an example: live demo CustomersModule is a lazy loading module i create a test service in customer module. const routes: Routes = [ { path: 'customers', loadChildren: () => import('./customers/customers.module').then(m => m.CustomersModule) }, { path: 'orders', loadChildren: () => import('./orders/orders.module').then(m => m.OrdersModule) }, { path: '', redirectTo: '', pathMatch: 'full' } ]; import { Injectable } from '@angular/core';

Angular 9 Service as Obesrvable in library

不想你离开。 提交于 2021-01-29 19:56:24
问题 I am working on multiapplication setup, where inside workspace 2 or 3 applications will share library with common and shared components. Currently, I am adding notifications service. Added (at least for now) as part of library main module. As base I am using this example from stackblitz or this one When calling notification service from inside library (which is inside the same module as notification.component: this._notificationSvc.success('Hello World','This is a success !'); the service

Angular: Metadata collected contains an error that will be reported at runtime: Lambda not supported

牧云@^-^@ 提交于 2020-06-27 14:50:08
问题 In my Angular app, I'm trying to use a factory provider in my module: export function getMyFactory(): () => Window { return () => window; } @NgModule({ providers: [ { provide: WindowRef, useFactory: getMyFactory() }, ], }) export class MyModule {} but this is failing with: Error encountered in metadata generated for exported symbol 'MyModule': Metadata collected contains an error that will be reported at runtime: Lambda not supported 回答1: I've found an easy solution reported on a thread from

Services reset on logout in Angular

 ̄綄美尐妖づ 提交于 2020-05-17 07:42:46
问题 I have some services in my app which have data that I'd like to reset when the user logs out. I have a parent component that is only shown when the user is logged in so if I include those services in the providers array of that component decorator, every time the user logs out, this component is destroyed, and the services instances are deleted as well. If another user logs in, this parent component would be created again with new services instances, thus fulfilling my needs. This works well,

What is the difference between providedIn any and root

随声附和 提交于 2020-02-28 09:51:49
问题 In Angular 9 the injectable decorator option providedIn has a new value called any . What is the difference between root and any ? Is a service considered a singleton in the case that I use any ? @Injectable({providedIn: 'any'}) class UsefulService { } 回答1: The difference between the root and any as per offical documentation : root : The application-level injector in most apps. platform : A special singleton platform injector shared by all applications on the page. any : The NgModule injector

What is the difference between providedIn any and root

你。 提交于 2020-02-28 09:51:27
问题 In Angular 9 the injectable decorator option providedIn has a new value called any . What is the difference between root and any ? Is a service considered a singleton in the case that I use any ? @Injectable({providedIn: 'any'}) class UsefulService { } 回答1: The difference between the root and any as per offical documentation : root : The application-level injector in most apps. platform : A special singleton platform injector shared by all applications on the page. any : The NgModule injector

What is the difference between providedIn any and root

跟風遠走 提交于 2020-02-28 09:51:12
问题 In Angular 9 the injectable decorator option providedIn has a new value called any . What is the difference between root and any ? Is a service considered a singleton in the case that I use any ? @Injectable({providedIn: 'any'}) class UsefulService { } 回答1: The difference between the root and any as per offical documentation : root : The application-level injector in most apps. platform : A special singleton platform injector shared by all applications on the page. any : The NgModule injector

The semantics of @Injectable(providedIn: 'root')?

我怕爱的太早我们不能终老 提交于 2020-01-14 12:17:06
问题 Just want to make sure I understand the semantics of @Injectable(providedIn: 'root') . Prior to Angular 6 if we import a module from NPM that contains a service we would declare that module in our app module such that the entire application has access to the service. Something like this: import { SomeNPModule } from '@ngx/SomeNPModule'; @NgModule({ imports: [ BrowserModule, SomeNPModule ] }) export class AppModule {} Now we can inject the SomeService that the module provides because we have

Cannot resolve parameters for ApplicationModule: (?)

喜欢而已 提交于 2019-12-07 04:53:25
问题 Pretty new to Angular. My app contains 1 service and 3 components. Compiled successfully. I'm getting this error and have no idea what went wrong: Uncaught Error: Can't resolve all parameters for ApplicationModule: (?). Debugging gave me very little idea.. It seems that the issue is related to NSLocaleLocalizations having null dependencies (if that makes sense) - see screenshot below: Here is some code: Please let me know if anything else is needed. Your help is much appreciated - thank you

APP_INITIALIZER raises “Cannot instantiate cyclic dependency! ApplicationRef_” when used with a custom Http provider that is redirecting

佐手、 提交于 2019-11-28 17:44:14
I am using a custom Http provider to handle API authentication error. In my CustomHttp, I need to redirect the user to the login page when a 401 status error is emitted by the API. That works fine! app.module.ts export function loadCustomHttp(backend: XHRBackend, defaultOptions: AppRequestOptions, router: Router, dataHelper: DataHelperService) { return new CustomHttp(backend, defaultOptions, router, dataHelper); } @NgModule({ // some declarations, imports, ... providers: [ // some services ... { provide: Http, useFactory: loadCustomHttp, deps: [XHRBackend, RequestOptions, Router,