question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ngOnInit losing reference to this

See original GitHub issue

So 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:closed
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
ryanpagercommented, Dec 21, 2016

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. 👍

3reactions
Hotellcommented, Dec 27, 2016

interesting! thx for creating workable demo with issue @fantasia949 .

Using pure es6 is still a edge point at this moment because browser support vary

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found