0

Full Disclaimer: I am very new to angular, like, I-just-started-yesterday-new kind of new.

I have this custom module:

foo.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FooComponent } from './foo.component';
import { FooService } from './foo.service';

@NgModule({
  imports: [
    CommonModule
  ],
  exports: [
    FooComponent
  ],
  declarations: [
    FooComponent
  ]
})
@NgModule()
export class FooModule {
  static forRoot(config: any): any {
    return {
      ngModule: FooModule,
      providers: [
        FooService
      ]
    }
  }
}

app.module.ts

In the app.module's imports section, I got this.

...
imports: [
  BrowserModule,
  FooModule.forRoot({id: 'SOME_ID'})
],
...

I want to pass the data in .forRoot(args) to the FooService so that I can initialize it.

After initializing the FooService I want to inject it into any component I want and use the methods inside it.

What I have in mind is something like this:

foo.service.ts

@Injectable()
export class FooService {
  // get the config from forRoot()
  constructor() {
    // initialize stuff here using
    // the config from forRoot()
  }

  doThis() {...}
  doThat() {...}
}

foo.component.ts

...
export class FooComponent implements OnInit {
  biz: any
  constructor(foo: FooService) {  
    this.biz = foo;
  }

  bar() {
    // acess stuff from FooService
    this.biz.doThis();
    this.biz.doThat();
  }

}
...
jofftiquez
  • 7,548
  • 10
  • 67
  • 121

0 Answers0