Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service is injectable without Injectable decorator #12098

Closed
jnizet opened this issue Oct 5, 2016 · 6 comments
Closed

Service is injectable without Injectable decorator #12098

jnizet opened this issue Oct 5, 2016 · 6 comments

Comments

@jnizet
Copy link
Contributor

jnizet commented Oct 5, 2016

I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

The documentation of Injectable says:

A marker metadata that marks a class as available to Injector for creation.
[...]
Injector will throw NoAnnotationError when trying to instantiate a class that does not have @Injectable marker

The CLI also adds Injectable to all the services it creates, and the tutorial explains that a service must have this annotation.

However, a simple experiment with a component using a service not annotated with Injectable works fine (see http://plnkr.co/edit/IJVf79C1D8bNVLg9Kh2f?p=preview for example). It seems that the Injectable annotation is actually only needed if the service itself has dependencies that must be injected in its constructor, but that doesn't match with what the documentation says.

Expected behavior

angular should throw an error when an injector is trying to create an instance of a service not decorated with Injectable. Or the documentation should be fixed to explain what Injectable really is for.

Minimal reproduction of the problem with instructions
Launch this plunkr: http://plnkr.co/edit/IJVf79C1D8bNVLg9Kh2f?p=preview. Note that FooService is successfully created by the injector and injected into the app component, even though it doesn't have the Injectable decorator.

What is the motivation / use case for changing the behavior?
Make the documentation match the actual behavior of angular, or vice-versa

Please tell us about your environment:
NA. Happens on all environments.

  • Angular version: 2.0.X

  • Browser: all

  • Language: TypeScript

@DzmitryShylovich
Copy link
Contributor

angular should throw an error when an injector is trying to create an instance of a service not decorated with Injectable. Or the documentation should be fixed to explain what Injectable really is for.

I think it's documentation issue.

@pkozlowski-opensource
Copy link
Member

I think it's documentation issue.

Yes, the documentation should be fixed. @Injectable is needed if you want to inject things into a service. @jnizet you please open documentation issue at https://github.com/angular/angular.io ? Thnx!

@jnizet
Copy link
Contributor Author

jnizet commented Oct 5, 2016

Documentation issue created: angular/angular.io#2546

@aluanhaddad
Copy link

I don't think it's a documentation issue necessarily, you could just as well argue that the decorator is poorly named and should be given a new name like provide.

@binDongKim
Copy link

The document may mislead us about the meaning of the @Injectable decorator. It should be fixed asap.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants