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

Instance bound methods do not work as hooks #16478

Closed
ValeriiVasin opened this issue May 2, 2017 · 3 comments
Closed

Instance bound methods do not work as hooks #16478

ValeriiVasin opened this issue May 2, 2017 · 3 comments
Labels
area: core Issues related to the framework runtime core: lifecycle hooks feature Issue that requests a new feature
Milestone

Comments

@ValeriiVasin
Copy link

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

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

Instance bound methods do not work as hooks. Here is a little code sample:

 import { Component } from '@angular/core';

  @Component({
    selector: 'my-app',
    template: '<h1>Hello {{name}}</h1>'
  })
  export class AppComponent { 
    name = 'Angular'; 
    
    ngOnInit = () => {
      this.name = 'World';
    }
  }

Current behavior
Hook is not executed, "Hello Angular" is displayed.

Expected behavior
Hook should be executed, and we would see "Hello World" instead. Or (if it is by design) - some message would be much appreciated in DEV mode. Smth like, "You are trying to use instance bound method for hooks. It will not work."

In the documentation i found only this note:

Angular instead inspects directive and component classes and calls the hook methods if they are defined. Angular finds and calls methods like ngOnInit(), with or without the interfaces.

Which is explaining it in some manner, but intention is not clear still.

Minimal reproduction of the problem with instructions
StackOverflow discussion:
http://stackoverflow.com/questions/43677584/angular2-instance-bound-methods-does-not-work-as-hooks

Plunkr: https://plnkr.co/edit/aZ1CYkDn06KUENmDW3a3?p=preview

What is the motivation / use case for changing the behavior?
It is kinda not transparent why Angular is behaving like that. I assume that it is by design for performance reasons. But it would be nice to mention that explicitly and provide a message for DEV mode to prevent others from spending time on such thing.

BTW, React is behaving in a predictable manner:

const { render } = ReactDOM;

class MyComponent extends React.Component {
  state = {
    name: 'Angular',
  };
  
  componentWillMount = () => {
    this.state.name = 'React';
  }
  
  render = () => {
    return <h1>Hello! {this.state.name}</h1>;
  }
}

render(
  <MyComponent />, 
  document.getElementById('app')
);

Hook is called and it displays "Hello! React" as you would expect.

JSBin for exploration: https://jsbin.com/yavokur/1/edit?js,output

Please tell us about your environment:
OSX El Capitan 10.11.6
VSCode
Express-based node.js server

  • Angular version: 2.0.X
    angular: 4.1.0

  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]

ALL

  • Language: [all | TypeScript X.X | ES6/7 | ES5]
    TypeScript 2.3+

  • Node (for AoT issues): node --version =
    node: 4.7.3

@matsko matsko added area: core Issues related to the framework runtime type: bug/fix labels May 3, 2017
@chuckjaz chuckjaz added feature Issue that requests a new feature and removed type: bug/fix labels Nov 28, 2017
@ngbot ngbot bot added this to the Backlog milestone Jan 23, 2018
@trotyl
Copy link
Contributor

trotyl commented Jul 23, 2018

Duplicate of #7270

@ValeriiVasin
Copy link
Author

seem to work as intended, as mentioned here

@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 Nov 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: core Issues related to the framework runtime core: lifecycle hooks feature Issue that requests a new feature
Projects
None yet
Development

No branches or pull requests

5 participants