Skip to content

Alternative way for depreciation of ComponentFactory in order to get componentFactory inputs #44926

Description

@ofirrifo

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

Yes

Description

Since Angular 13 the ComponentFactory is deprecated.

Since it depreciated I need an alternative way to get the inputs property that I used to get from the instance of ComponentFactory this.componentFactory.inputs

Here is example of code where I'm using the inputs

@Directive({
  selector: '[dynamicComponentDirective]',
})
export class SingDynamicComponentDirective implements OnInit {
  @Input() dynamicComponentClass!: Type<any>;
  @Input() inputs: any;

  componentFactory: ComponentFactory<any> | undefined = undefined;
  component!: ComponentRef<any> | null;

  constructor(private resolver: ComponentFactoryResolver, private container: ViewContainerRef) {}

  ngOnInit(): void {
    this.container.clear();
    this.componentFactory = void 0;
    this.component = null;
    this.componentFactory = this.resolver.resolveComponentFactory<any>(this.dynamicComponentClass);
    this.component = this.container.createComponent(this.componentFactory);

    this.updateComponentInputs();
  }

  /**
   * update component inputs
   */
  updateComponentInputs(): void {
    const validInputs = this.inputs && isObject(this.inputs) && Object.keys(this.inputs).length;
    if (this.component && validInputs && this.componentFactory && this.componentFactory.inputs) {
      this.componentFactory.inputs.forEach((input: any) => {
        if (this.component && this.inputs.hasOwnProperty(input.propName)) {
          this.component.instance[input.propName] = this.inputs[input.propName];
        }
      });
    }
  }
}

Now that ComponentFactory is deprecated what is the alternative to get the inputs property?

Please provide a link to a minimal reproduction of the bug

above there is a code with the example of my problem

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 13.2.0
Node: 16.10.0
Package Manager: npm 7.6.0
OS: darwin x64

Angular: 13.2.0
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, material, platform-browser, platform-browser-dynamic
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1302.0
@angular-devkit/build-angular   13.2.0
@angular-devkit/core            13.2.0
@angular-devkit/schematics      13.2.0
@schematics/angular             13.2.0
ng-packagr                      13.2.0
rxjs                            7.5.2
typescript                      4.5.5

Anything else?

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions