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

docs: fix bug in code examples for downgrading a service #19371

Closed

Conversation

bertyhell
Copy link
Contributor

No description provided.

@gkalpak
Copy link
Member

gkalpak commented Sep 25, 2017

The service is indeed called heroes in AngularJS-land. This change seems invalid.

@bertyhell
Copy link
Contributor Author

The service is indeed called heroes in AngularJS-land. This change seems invalid.

If that is the case, the documentation is even more confusing, since it states the opposite here:
https://angular.io/guide/upgrade#making-angularjs-dependencies-injectable-to-angular

In these situations, it is possible to upgrade an AngularJS provider to Angular. This makes it possible to then inject it somewhere in Angular code. For example, you might have a service called HeroesService in AngularJS:

@gkalpak
Copy link
Member

gkalpak commented Sep 25, 2017

It is indeed somewhat confusing, because we never show how the service is registered in AngularJS. In both Angular and AngularJS there are two things that identify a registered service:

  1. The injection token, i.e. an identifier that we will use to request the service from the injector.
  2. The service class (which will be used by the injector to create an instance of the service).

In Angular, it is common to use the class itself as the injection token:

providers: [
  {provide: MyClass, useClass: MyClass},
  // or the shorthand:
  MyClass
]

This is possible, because Angular's injection system can accept any token as identifier.

In AngularJS on the other hand, we only accept string identifiers as token. Thus you are forced to use two different things: a string (for the injection token) and a class (for the service)

.service('myServiceId', MyServiceClass)

It is common in AngularJS apps to chose a service name for the token (e.g. user) and append the Service suffix to create the class name:

.service('user', class UserService { ... })

Hopefully that clears it up a bit. Feel free to update to clarify things (as long as the change is valid 😃).

@bertyhell
Copy link
Contributor Author

Ok, I changed the token name back to the original and added a note to explain the 'heroes' string.
Does that work for you?

https://github.com/angular/angular/pull/19371/files#diff-33c8eb0a067223c3fbf3dd45ec689efdR783

@trotyl
Copy link
Contributor

trotyl commented Sep 25, 2017

This is possible, because Angular's injection system can accept any token as identifier.

Hi @gkalpak , sorry for off topic, I guess there're some different voices on #15319 (comment), may I know what is the expected validity for DI token now (or planned future)?

@bertyhell
Copy link
Contributor Author

@trotyl Good point, the question is: After the 5.0 release will people be upgrading their app from 1.x => 4.x or 1.x => 5.x.

Either way, the docs will have to be updated to reflect this, but that's beyond the scope of this PR.

@gkalpak
Copy link
Member

gkalpak commented Sep 25, 2017

@trotyl, yes, things might change in future version. I am not sure what the current state is.
@bertyhell, what we have in the docs now will work with 4.x and 5.x alike, since we are using a Type (HeroesService) as token (in this case at least).

@@ -5,6 +5,7 @@ import { Hero } from '../hero';

@Component({
selector: 'hero-detail',
providers: [HeroesService],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

@bertyhell bertyhell Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gkalpak Without this change I get this error in the app I'm trying to migrate:

ERROR Error: Uncaught (in promise): Error: [$injector:unpr] 
Unknown provider: heroesServiceProvider <- heroesService

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example provides the service on the NgModule. Providing it on the component too, will create a separate instance for the component (different than what the rest of the app is using). This is not what we want here (but might be what you want in your app).

To share the same instance of the service across components, put it on the providers array of the NgModule.

@bertyhell
Copy link
Contributor Author

To share the same instance of the service across components, put it on the providers array of the NgModule.

I modified the code and added some more description text to the readme.

Copy link
Contributor

@kapunahelewong kapunahelewong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates @bertyhell. Just a couple of comments for you.


Note that the 'heroes' string inside the factory refers to the AngularJS `HeroesService`.
It is common in AngularJS apps to chose a service name for the token (e.g. heroes)
and append the Service suffix to create the class name (e.g. heroesService)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please change "class name (e.g. heroesService)" to "class name, for example heroesService"? The reasons for changes are:

  1. We are trying to minimize parentheticals whenever we can.
  2. We're replacing i.e. and e.g. with "that is" and "for example" since people often confuse them.
  3. For consistency, heroesService should be in code font, thus the back ticks.

<code-example path="upgrade-module/src/app/ajs-to-a-providers/app.module.ts" region="register" title="app.module.ts">
</code-example>

You can then inject it in Angular using it's class as a type annotation:
Then use the service inside your component by injecting it in the component constructor using it's class as a type annotation:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's --> its

I know this was already there, but since you're on the line, would you mind deleting the apostrophe?

@bertyhell
Copy link
Contributor Author

@kapunahelewong Sorry for the long delay. I missed the email notification about your comments.

@ngbot
Copy link

ngbot bot commented Jan 30, 2018

Hi @bertyhell! This PR has merge conflicts due to recent upstream merges.
Please help to unblock it by resolving these conflicts. Thanks!

1 similar comment
@ngbot
Copy link

ngbot bot commented Jan 30, 2018

Hi @bertyhell! This PR has merge conflicts due to recent upstream merges.
Please help to unblock it by resolving these conflicts. Thanks!

@kapunahelewong
Copy link
Contributor

kapunahelewong commented Feb 23, 2018

Hey @bertyhell, if you'd like to rebase, I'd be happy to approve :)

EDIT: oh, and squash.

@jenniferfell jenniferfell added state: community Someone from the Angular community is working on this issue or submitted this PR and removed state: community Someone from the Angular community is working on this issue or submitted this PR labels Jul 23, 2018
@brandonroberts brandonroberts added target: patch This PR is targeted for the next patch release docsarea: upgrading-AJS labels Aug 6, 2018
@brandonroberts
Copy link
Contributor

@gkalpak are there any changes left on this from your point of view?

@@ -778,16 +778,25 @@ delete them once the upgrade is over.
It's also recommended to export the `heroesServiceFactory` function so that Ahead-of-Time
compilation can pick it up.

<div class="l-sub-section">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change l-sub-section to alert is-helpful

Copy link
Contributor

@brandonroberts brandonroberts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor css change

Copy link
Member

@gkalpak gkalpak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a couple of minor comments. LGTM otherwise.

<div class="l-sub-section">

Note that the 'heroes' string inside the factory refers to the AngularJS `HeroesService`.
It is common in AngularJS apps to chose a service name for the token, for example heroes,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chose --> choose
for example heroes --> for example "heroes"


Note that the 'heroes' string inside the factory refers to the AngularJS `HeroesService`.
It is common in AngularJS apps to chose a service name for the token, for example heroes,
and append the Service suffix to create the class name, for example `heroesService`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Service --> "Service"
Also, classes (or constructor functions) start with an uppercase letter (by convention). So HeroesService, not heroesService.

<code-example path="upgrade-module/src/app/ajs-to-a-providers/ajs-upgraded-providers.ts" title="ajs-upgraded-providers.ts">
</code-example>

You can then add the service to Angular by adding it to the `@NgModule`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change "add the service" to "provide the service".

@brandonroberts
Copy link
Contributor

@gkalpak this one is ready for another review

@brandonroberts brandonroberts changed the title docs(upgrade): Fix bug in code examples for downgrading a service docs: fix bug in code examples for downgrading a service Aug 16, 2018
Copy link
Member

@gkalpak gkalpak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#19371 (comment) hasn't been fully addressed. Other than that LGTM.

@gkalpak gkalpak added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Aug 20, 2018
@brandonroberts brandonroberts removed the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Aug 20, 2018
@brandonroberts brandonroberts added the action: merge The PR is ready for merge by the caretaker label Aug 21, 2018
@jasonaden jasonaden closed this in 69b57b2 Aug 21, 2018
jasonaden pushed a commit that referenced this pull request Aug 21, 2018
petebacondarwin pushed a commit to petebacondarwin/angular that referenced this pull request Aug 22, 2018
FrederikSchlemmer pushed a commit to FrederikSchlemmer/angular that referenced this pull request Jan 3, 2019
@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
action: merge The PR is ready for merge by the caretaker cla: yes state: community Someone from the Angular community is working on this issue or submitted this PR target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants