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

Is this a good way to inject deps to a class? #4

Open
Brooooooklyn opened this issue Apr 15, 2016 · 0 comments
Open

Is this a good way to inject deps to a class? #4

Brooooooklyn opened this issue Apr 15, 2016 · 0 comments

Comments

@Brooooooklyn
Copy link
Member

Brooooooklyn commented Apr 15, 2016

Foo.ts

import {inject} from 'ng-injector'
@inject([
  '$http'
])
export default class Foo {
  constructor(private userId: string) {}

  getBirth() {
    return this.$http.get(`api.test.com/${this.userId}/age`)
  }
}

Bar.ts

import {inject} from 'ng-injector'
import Foo from './Foo'

@inject([
  '$state'
])
class Bar {
   birth: string
   private foo: Foo

   constructor() {
     const userId = this.$state.params._id
     // see here
     this.foo = new Foo(userId)
   }

  onInit() {
    return this.foo
      .getBirth()
      .then(birth => this.birth = birth)
  }
}

angular.module('App').controller('Bar', Bar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant