user14517108
user14517108

Reputation:

How to pass parameter in constructor?

I have an abstract class:

export abstract class SidebarCreator {
    public abstract factoryMethod(): SidebarMenu;
}

And realization:

export class ApplicationSidebarCreator extends SidebarCreator {
    constructor(private application: IApplication) {
        super();
    }
}

Inside component I use it:

@Component({
   providers: [{ provide: SidebarCreator, useClass: ApplicationSidebarCreator }]
})

  @Input() application: IApplication;

How to do pass variable application inside component to constructor of ApplicationSidebarCreator?

Upvotes: 0

Views: 63

Answers (1)

MoxxiManagarm
MoxxiManagarm

Reputation: 9124

I don't think what you designed is possible. application is a property on input which can change now and then. I think you will need to implement a setApplication method within the service and call it when application input changes.

export class ApplicationSidebarCreator extends SidebarCreator {
    private application: IApplication

    constructor() {
        super();
    }

    setApplication(application: IApplication) {
      this.application = application;
    }
}
@Component({
   providers: [{ provide: SidebarCreator, useClass: ApplicationSidebarCreator }]
})

  @Input() application: IApplication;

  constructor(private sidebarCreator: SidebarCreator) { }

  ngOnChanges(changes: SimpleChanges) {
    if (changes['application']) {
      this.sidecarCreator.setApplication(changes['application'].currentValue);
    }
  }

Upvotes: 1

Related Questions