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 · 5 comments

Comments

Projects
None yet
5 participants
@jnizet
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

This comment has been minimized.

Contributor

DzmitryShylovich commented Oct 5, 2016

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

This comment has been minimized.

Member

pkozlowski-opensource commented Oct 5, 2016

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

This comment has been minimized.

Contributor

jnizet commented Oct 5, 2016

Documentation issue created: angular/angular.io#2546

@aluanhaddad

This comment has been minimized.

aluanhaddad commented Oct 18, 2016

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

This comment has been minimized.

binDongKim commented Jul 27, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment