145 questions
3
votes
0
answers
8k
views
Angular Unit Testing with Dynamic Component Failed with error TypeError: Cannot read property 'ɵcmp' of undefined
list of Angular dependency
"@angular/common": "~9.1.1",
"@angular/compiler": "~9.1.1",
"@angular/core": "~9.1.1",
"@angular/forms": "~9.1.1",
"@angular/platform-browser": "~9.1.1"...
3
votes
1
answer
2k
views
How to use provideIn: 'platform' in angular 9
I have been reading the angular 9 documentation, and it states you can use the provideIn: 'platform' to share a singleton service across two angular applications, but I was not able to find a good ...
2
votes
1
answer
782
views
Angular2 Dependency Injection by Factory Method using Interface
I'm trying to use Angular2 dependency injection but get the following error message:
error NG2003: No suitable injection token for parameter 'service' of class 'PaymentService'
app.module.ts - ...
0
votes
0
answers
46
views
How to access DI from model in a generic way?
I have the following class (only the relevant part):
import { AppInjector } from '@app/shared/utility/app-injector';
import { HttpService } from '@app/shared/services/http.service';
export class ...
0
votes
1
answer
2k
views
Injecting different parent components into child component
Im trying to solve the following situation:
I have a child component that will be used in multiple parent components but this child component needs to have access to the parent component (namely ...
27
votes
13
answers
68k
views
This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid
In my Angular 9 app, I have an abstract class:
export abstract class MyAbstractComponent {
constructor(
protected readonly cd: ChangeDetectorRef,
) {
super();
}
// ...
}
and a ...
1
vote
1
answer
385
views
providedIn: LazyModule fails with no provider error
I would like to refer the providedIn option to an Angular module providing both declarables and dependencies. I created an additional module SharedModule to serve as a sub-module of the lazy module ...
16
votes
3
answers
4k
views
Override Angular providers provided in root
I import a modal library of ng-bootstrap in a lazy module.
@NgModule({imports: [NgbModalModule]})
This library has a NgbModal service provided in root.
@Injectable({providedIn: 'root'})
class ...
28
votes
4
answers
25k
views
What is the difference between providedIn any and root
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?
@...
0
votes
1
answer
958
views
How do I refer to my config in my imports?
I have added AngularFire to my project. In its readme I was instructed to add the AngularFireModule.initializeApp(...) to the imports of my AppModule
In examples people use the environment constant, ...
1
vote
2
answers
947
views
Default values config using forRoot works in Dev but not in Prod (Aot)
The following code works fine in Dev, but not in Prod mode:
Module
export class SpinnerModule {
static forRoot(config?: SpinnerConfig): ModuleWithProviders {
return {
ngModule: ...
5
votes
2
answers
7k
views
How to provide an `InjectionToken` that has its own `factory`?
Consider the following InjectionToken for the type Foo:
export const FOO = new InjectionToken<Foo>(
'foo token',
{ factory: () => new Foo() });
Now assume I was crazy enough to aim for ...
5
votes
1
answer
339
views
Is there a way to enforce typesafety for Angular providers with dependency injection?
I would like to use dependency injection to swap the implementation of the provider but can't figure out how to make that type safe.
@Injectable()
class ExampleService {
exampleMethod(): string {
...
2
votes
0
answers
4k
views
Angular8 - how to pass input to CDK Overlay by to Custom Injector with InjectionToken and ComponentPortal
I have 2 components: A home component and a confirmation component (overlay).
When a button is clicked on the home component, it should trigger the confirmation component to open as a Angular CDK ...
1
vote
0
answers
34
views
Decide at startup time which implementation to use for a service provided via Dependency Injection
Let's assume I have a Front End App which needs to fetch data from some backend. And let's assume that there are 2 different backend REST APIs implementations available, BackEnd1 and BackEnd2. They ...
1
vote
2
answers
3k
views
Angular 8 inject pipe into another pipe does not work
After upgrading from Angular 7 to Angular 8 my pipe stopped to work with error:
TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
10 | export class APipe ...
1
vote
2
answers
4k
views
Angular inject a component into an attribute directive
Tl;dr: How do I provide a visible component as a dependency for a directive? Naturally the component has to get initialized before the directive, but it has to be the same instance that gets displayed ...
2
votes
1
answer
2k
views
How to inject parent component service into dynamic component in Angular?
I am using Angular 8 and I have dynamically created component. I have parent component with the service into providers array. How I can inject the same instance of parent MyService into a dynamic ...
13
votes
2
answers
10k
views
How to inject a service into a constant in angular
I'm trying to export a constant in angular and I need to set a key whose value will be returned from a service. I tried with following code:
This is my user-config.ts file:
export const USER_CONFIG = {...
2
votes
3
answers
2k
views
When does a service get created by dependency injection
I have an angular service containing some init code in its constructor. When is this code called, i.e. when is the service created?
Already when the service is provided in some module?
Or not until ...
13
votes
4
answers
13k
views
Angular: Metadata collected contains an error that will be reported at runtime: Lambda not supported
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,...
0
votes
1
answer
76
views
Angular - Providing asynchronous to root providers
My goal is to load a configuration file (from assets) which includes settings I need for my http service.
Currently I have an APP_INITIALIZER service which loads the file and sets it in the service ...
1
vote
1
answer
461
views
Angular update 8.1 - circular dependency warning
Since updating to Angular 8.1 (8.0 worked), I'm getting a circular dependency warning that doesn't really make sense to me.
I have an Injectable Service SelectService that is provided in some ...
1
vote
1
answer
2k
views
Use an angular service which is not injected anywhere
Suppose you have an Angular service (ZombieService) which, for example, monitors an other service.
And, ZombieService isn't injected anywhere DEMO
The problem is that, when you do not inject a ...
0
votes
0
answers
447
views
Pass string value in providers and pass value to super of Base Service
I am creating a generic crud service that i want to use throughout my angular application in various feature modules.
To achieve this i need to pass a string value to a service in providers: of an ...
0
votes
1
answer
3k
views
Transitively inject injection token into a service defined in library, used within an angular application
I've created an angular library, say library1, which defines and exposes a service APIService. The APIService has a constructor argument (URL: string), which is injected with an Injection token ...
0
votes
1
answer
1k
views
Get value for provider from service
I am trying to assign some value (which I got from http request) from another service to provider token in app.module.ts.
My service looks like this:
@Injectable()
export class AppConfigService {
...
1
vote
0
answers
326
views
How to proxy a component that uses DI?
I'm trying to create a proxy for Angular components. Starting from this solution:
https://github.com/Microsoft/TypeScript/issues/4890#issuecomment-141879451
I ended up with this:
interface Type<T&...
1
vote
1
answer
3k
views
New instance of Angular singleton service being created in another singleton service
I am new to Angular and am trying to figure out how to inject my Angular service (game service) into another angular service which will be a Resolver (game resolver). Only one instance of my game ...
6
votes
0
answers
2k
views
How to set config (or useValue) for imported modules from a component?
We're well aware that there are various ways of setting config for imported modules. We have '.forRoot()', 'useValue', 'useClass' and such to be used in the importing module.
Say for example, we want ...
1
vote
0
answers
352
views
Angular dependency injection of a component taking place without decorators
I came across some code in which some components have access to their parent component instance but I could not figure out how the "injection" takes place.
I expected the code to fit in one of those ...
8
votes
2
answers
3k
views
Pass configuration data to a dependency of a angular library using "forRoot"
I've created two Angular libraries, one has the other as a dependency.
the dependency needs to be configured using the forRoot method.
how can i pass the configuration data from the parent library to ...
1
vote
2
answers
2k
views
Provide Unique Service Instance in Every Component
I have a situation where I want to be able to have multiple widget components used on the page at the same time. I want to isolate the ContainerComponent dependencies so that each ContainerComponent ...
0
votes
1
answer
375
views
Angular service global injection
Is there a way to access a field of a service without injecting the service into every component? or equivalently: does some concept of 'global' service exist in Angular 6?
My use case is as follows: ...
2
votes
1
answer
2k
views
Abstract method implementation in component has undefined dependencies
Abstract class:
export abstract class LanguageChangeAware {
private sub: Subscription;
protected language: string;
protected constructor(protected eventService: EventService) {
...
0
votes
2
answers
79
views
Not able to inject Dependency Injection in angular4
i have a following class like this
import { Injectable, Inject } from '@angular/core';
@Injectable()
export class moviindustry {
constructor(private music:musicindustry) { }
producer() {
...
2
votes
1
answer
1k
views
How to inject a service into a class that must be instantiated in app module in angular 7?
I am using ngxs in a project and I want to use the ngxs logger plugin, but I want to override the logger itself so that I am able to log to the backend. For this I need to inject my RestService in my ...
2
votes
1
answer
1k
views
Angular Universal, How to isolate module with declarations from server-side execution?
I have installed ng2-pdf-viewer to show documents within angular 6 app.
To make it work I need to import PdfViewerModule in FeatureModule where I want to use <ng2-pdf-viewer> component.
The ...
5
votes
2
answers
4k
views
Cannot resolve parameters for ApplicationModule: (?)
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 ...
2
votes
1
answer
3k
views
How to override services injected to angular component
We're building a cms driven application framework with Angular. We deliver multiple packages that can be used by customers to further customise the ui or logic. Components to that reason will delegate ...
1
vote
1
answer
3k
views
Can't inject `Route` nor `ActivatedRouteSnapshot` into `HttpInterceptor`
Simplified version of the code:
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
constructor(
private readonly router: Router,
private readonly activatedRouteSnapshot: ...
19
votes
2
answers
9k
views
Angular 6. Is it possible to inject service by condition?
In my angular app (with the angular material) I have a filter panel and I want besides select to be able to make autocomplete (user input value and it sends to the back-end, whereby $regexp query we ...
6
votes
1
answer
7k
views
The semantics of @Injectable(providedIn: 'root')?
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 ...
1
vote
2
answers
737
views
Invalid provider error when injecting service into service in Angular 2
I currently have a module setup like below (exerpt);
AppModule
RoutingModule
AuthRouteGuard
AuthModule
LoginFormComponent
AuthService
I've defined my AuthService (...
70
votes
3
answers
37k
views
APP_INITIALIZER raises "Cannot instantiate cyclic dependency! ApplicationRef_" when used with a custom Http provider that is redirecting
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!
...