[go: up one dir, main page]

Skip to content

Custom configuration

Michael Hladky edited this page Apr 20, 2018 · 4 revisions

Custom configuration

There is a Service called StarRatingConfigService which you can use to configure all star rating conponents.

You can do so by overriding the StarRatingConfigService in your modules providers section.

Override Globally

Create a new service by using the cli ng generate service pathToFolder/serviceName --module=pathToModule/moduleName.ts Extend StarRatingConfigService and override some properties:

@Injectable()
export class CustomConfigService extends StarRatingConfigService {

  constructor() {
    super();
    this.numOfStars = 3;
    this.staticColor = 'positive';
  }
}

Override dependencies

In your module implement the needed providers:

@NgModule({
  imports: [
    [...]
    StarRatingModule.forRoot()
  ],
  [...]
  providers: [
    {
      provide: StarRatingConfigService, useClass: CustomConfigService
    }
  ]
})
export class AppModule {}

Override in a FeatureModules

Create a new service by using the cli ng generate service pathToFolder/serviceName --module=pathToModule/moduleName.ts Extend StarRatingConfigService and override some properties:

@Injectable()
export class CustomConfigService extends StarRatingConfigService {

  constructor() {
    super();
    this.numOfStars = 3;
    this.staticColor = 'positive';
  }
}

Override dependencies

In your feature module implement the needed providers:

@NgModule({
  imports: [
    [...]
    StarRatingModule.forRoot()
  ],
  [...]
  providers: [
    {
      provide: StarRatingConfigService, useClass: CustomConfigService
    }
  ]
})
export class FeatureModule {}

Override Locally

There is also the possibility to override is in the components providers section.

@Component({
  selector: 'my-component',
  template: `
  <h2>Custom local config</h2>
  <star-rating></star-rating>`,
  providers: [
    {
      provide: StarRatingConfigService, useClass: CustomLocalConfigService
    }
  ]
})
export class CustomLocalConfigComponent { 
}