Skip to content

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

@Brooooooklyn

Description

@Brooooooklyn

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions