Skip to content

Semantics of Arguments Passed to Constructor of Component #23463

Closed

Description

@sayasree

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[X] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:

Current behavior

In the URL: https://angular.io/guide/architecture-components
at the place where the "class HeroListComponent" is defined, the explanation for the semantics of passing the argument "private service: HeroService" to the constructor method is missing.
Someone coming from the OOP background, they think that it is just a formal parameter and expects it to be initialized with the instance of the object created with new operator.

Expected behavior

Looks like, in Angular, by virtue of specifying it as a formal parameter to constructor method it automatically does the following:
private service: HeroService;
constructor() {
service = new HeroService();
}
If this is the case, in case some parameters to be passed while constructing as HeroService(p1,p2,p3), how it is done when it can be done in the constructor() syntax.

Minimal reproduction of the problem with instructions

None

What is the motivation / use case for changing the behavior?

Not Applicable

Environment


Angular version: X.Y.Z


Browser:  Not Applicable
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:  Not Applicable
- Node version: XX  
- Platform:  

Others:  Not Applicable

Activity

  1. trotyl commented on Apr 20, 2018

    @trotyl
    Contributor

    This is normal TypeScript semantics, Angular was not involved at all.

  2. jotatoledo commented on Apr 20, 2018

    @jotatoledo
    Contributor

    Someone coming from the OOP background, they think that it is just a formal parameter and expects it to be initialized with the instance of the object created with new operator.

    Im pretty sure that most devs will recognize that the component is using constructor injection.

    Looks like, in Angular, by virtue of specifying it as a formal parameter to constructor method it automatically does the following:
    private service: HeroService;
    constructor() {
    service = new HeroService();
    }

    Not sure where did you got this idea from, but its incorrect. @trotyl already pointed out how parameter properties work.

  3. sayasree commented on Apr 20, 2018

    @sayasree
    Author
  4. added this to the needsTriage milestone on Apr 20, 2018
  5. jenniferfell commented on Apr 21, 2018

    @jenniferfell
    Contributor

    Thank you for the original comment and clarifications re typescript experience. Given that this architecture doc is part of the introductory material, we'll look at making it more clear for those who are learning typescript and angular together.

  6. jenniferfell commented on Apr 21, 2018

    @jenniferfell
    Contributor

    docarea=architecture
    filename=guide/architecture-components.md

  7. removed this from the needsTriage milestone on Apr 24, 2018
  8. 5 remaining items

  9. added a commit that references this issue on Jul 10, 2018
    8887d75
  10. added 2 commits that reference this issue on Jul 30, 2018
    5399c17
  11. angular-automatic-lock-bot commented on Sep 13, 2019

    @angular-automatic-lock-bot

    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.

  12. locked and limited conversation to collaborators on Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions