Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何将@Input与使用ComponentFactoryResolver创建的组件一起使用?_Angular - Fatal编程技术网

Angular 如何将@Input与使用ComponentFactoryResolver创建的组件一起使用?

Angular 如何将@Input与使用ComponentFactoryResolver创建的组件一起使用?,angular,Angular,是否有一种方法可用于在动态创建的Angular 2组件上定义@Input属性 我正在使用创建容器组件中的组件。例如: let componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentName); let componentRef = entryPoint.createComponent(componentFactory); 其中“entryPoint”在组件HTML中类似于以下内容:

是否有一种方法可用于在动态创建的Angular 2组件上定义@Input属性

我正在使用创建容器组件中的组件。例如:

let componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentName);

let componentRef = entryPoint.createComponent(componentFactory);
其中“entryPoint”在组件HTML中类似于以下内容:

<div #entryPoint></div>
这很好,但我找不到一种方法使@Input属性在新创建的组件上工作。我知道您可以显式地在组件类上设置公共属性,但这似乎不适用于ng reflect。在进行此更改之前,我有一个用“@Input()”修饰的“selected”属性,该属性导致Angular向DOM添加以下内容:

<my-component ng-reflected-selected="true"></my-component>

有了它,我可以动态更新标记以切换CSS类:

<div class="header" [class.active-header]="selected === true"></div>

通过一些搜索,我能够找到一种方法使“@Output”按预期工作,但我还没有找到任何有关@Input的内容


请告诉我附加的上下文是否有用,我很乐意添加它。

否,Angular2绑定仅适用于静态添加到组件模板中的组件和指令

对于所有其他情况,请使用共享服务,如中所述

你也可以使用

let componentRef = entryPoint.createComponent(componentFactory);
componentRef.instance.someProp = 'someValue';
componentRef.instance.someObservableOrEventEmitter.subscribe(data => this.prop = data);

如何更改组件生成的DOM元素的.style属性?就是这个.cmpRef.\u hosteelement.nativeElement.style。。。不使用私有变量,您可以添加
@HostBinding('style.border')borderStyle:string到组件,然后将其设置为
componentRef.instance.borderStyle='solid 3px red'不确定这是否是您想要的。否则,动态设置样式的选项就不多了;假设dynamicRef=componentRef,如有;dynamicRef.instance.change.subscribe(x=>console.log(x));不确定这是否是一个好主意,因为它破坏了动态创建组件的类型安全。我认为当我这样做时,componentRef.instance.someProp='someValue';不会在子组件上触发ngOnChanges。这项拖车工作还需要做更多的步骤吗?
let componentRef = entryPoint.createComponent(componentFactory);
componentRef.instance.someProp = 'someValue';
componentRef.instance.someObservableOrEventEmitter.subscribe(data => this.prop = data);