question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useFactory in @Injectable doesn't work using object's shorthand method definition

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/core

Is this a regression?

Yes, the previous version in which this bug was not present was: I don't know

Description

useFactory doesn’t work when I use it like this:

@Injectable({
  providedIn: 'root',
  useFactory() {
    console.log('this useFactory should be called, but... nope :`(')

    return {}
  },
})
class TestService {
  constructor() {
    console.log('this constructor should not be called!!! but it's');
  }
}

Using an arrow function, an anonymous function or an exported function work (see repro). But using an arrow function angular language service complains… it works at runtime and building, but it’s annoying image

πŸ”¬ Minimal Reproduction

this repro uses 9.1.1, but it doesn’t work with latest (10.0.12) either

https://ng-run.com/edit/zwh0lwIk3NYX9tKWSUMt

πŸ”₯ Exception or Error





🌍 Your Environment

Angular Version:




Angular CLI: 10.0.7
Node: 14.6.0
OS: linux x64

Angular: 10.0.12
... animations, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic, router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.1000.4
@angular-devkit/build-angular     0.1000.7
@angular-devkit/build-optimizer   0.1000.7
@angular-devkit/build-webpack     0.1000.7
@angular-devkit/core              10.0.4
@angular-devkit/schematics        10.0.7
@angular/cdk                      10.1.3
@angular/cli                      10.0.7
@ngtools/webpack                  10.0.7
@schematics/angular               10.0.7
@schematics/update                0.1000.7
rxjs                              6.6.2
typescript                        3.9.7
webpack                           4.43.0

Anything else relevant?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
djleonskennedycommented, Aug 28, 2020

Useless functionality, can be removed

0reactions
tonivj5commented, Aug 25, 2020

No problem, thanks for confirm! πŸ˜„

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring dependency providers - Angular
The expanded provider configuration is an object literal with two properties: ... useFactory - allows you to define a function that constructs a...
Read more >
Angular Providers: useClass, useValue, useFactory ...
It can create a dependency from the existing service class ( useClass ). It can inject a value, array, or object ( useValue...
Read more >
Can't inject service into Angular Provider UseFactory
The answer, in my case, was very easy! The provider code needs a property called deps with the types of the dependencies I...
Read more >
Dependency Injection in Angular - Parser
useFactory. The useFactory allows you to create a dependency object by calling a factory function. It is convenient if you don't know whatΒ ......
Read more >
Dependency providers - Angular Hispano
Not all dependencies are classes. Sometimes you want to inject a string, function, or object. Apps often define configuration objects with lots of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found