ngOnInit losing reference to this
See original GitHub issueSo I am currently converting our angular-1 app over to use ng-metadata so we can start the transition into angular-2 style code. Everything seems to work ok, but our lifecycle events cannot access the this property, it is always undefined.
Is there something im missing? An example of our code that has problems,
import { Component, Inject, OnInit } from 'ng-metadata/core'
import * as template from "./template.html"
@Component({
selector: 'login-route',
template: template
})
export class LoginRoute implements OnInit {
constructor(@Inject('$http') private $http: ng.IHttpService) {
console.log('this.$http is ok', this.$http)
}
ngOnInit() {
console.log('this.$http is undefined', this.$http)
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Why a lost reference to variable in Angular? - Stack Overflow
When you change the app.selected = !app.selected you change the value of a property from your @input variable.
Read more >OnInit - Angular
A lifecycle hook that is called after Angular has initialized all data-bound properties of a directive. Define an ngOnInit() method to handle any...
Read more >What to do when “this” loses context - freeCodeCamp
A way to fix the problem is to create new functions in the constructor using bind(this) . constructor(){ super(); this.todos = []; this....
Read more >3 Ways to Pass Async Data to Angular 2+ Child Components
... initialization time - during ngOnInit life cycle hook (refer here for ... One thing I don't like about this solution is that...
Read more >48 answers on StackOverflow to the most popular Angular ...
ngOnInit is a lifecycle hook called by Angular2 to indicate that Angular is ... How do I catch hold of a reference of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Don’t worry about it @Hotell, we’ve ditched ng-metadata as a team and we’ll use something else. Thanks for the condescending response at the end. 👍
interesting! thx for creating workable demo with issue @fantasia949 .
Using pure es6 is still a edge point at this moment because browser support vary