Is there a method that can be used to define an @Input property on an Angular 2 component that's created dynamically?
I'm using the ComponentFactoryResolver to create components in a container component. For example:
let componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentName);
let componentRef = entryPoint.createComponent(componentFactory);
Where "entryPoint" is something like this in the component HTML:
<div #entryPoint></div>
And defined in my container component with:
@ViewChild('entryPoint', { read: ViewContainerRef } entryPoint: ViewContainerRef;
This works well, but I can't find a way to make an @Input property work on the newly created component. I know that you can explicitly set public properties on the component class, but this doesn't seem to work with ng-reflect. Prior to making this change I had a "selected" property decorated with "@Input()" that caused Angular to add the following to the DOM:
<my-component ng-reflected-selected="true"></my-component>
With this in place I was able to dynamically update the markup to switch a CSS class:
<div class="header" [class.active-header]="selected === true"></div>
Based on some searching I was able to find a method to make "@Output" work as expected, but I've yet to find anything for @Input.
Let me know if additional context would be helpful and I'd be happy to add it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…