我正在使用动态组件创建(resolveComponentFactory) ,因此它在静态@Input()属性下工作良好。但对于动态设置器而言,它不起作用。我不能这样做this.componentReference.instance[myPropVar]= someValue
与创建内部组件的setter。动态组件属性的设置器Angular 2
这可能吗?谢谢!在我的动态组件
的属性设置为:
@Input() set lockMessage(val: any) {
if (val) {
console.log("Visit details -> ", val);
}
}
这就像在该职位 Angular 2 dynamic component creation
相同的,但我想添加一些财产有二传手我的动态创建的组件。
P.S.是。我试图设置我的财产动态组件与该建设
/**
* public updateSingleComponentProp -> update single prop on component instance
* @param prop {string} -> property name
* @param val {any} -> property value
* @returns {void}
*/
public updateSingleComponentProp(prop: string, val: any): void {
let compInstance = this.componentReference.instance;
compInstance[prop] = val;
if (compInstance.hasOwnProperty(prop) || compInstance.hasOwnProperty('_' + prop))
compInstance[prop] = val;
else
throw new Error("Component doesn't have this property -> " + prop);
}
并且它抛出一个错误,因为该属性不存在。我检查了组件实例并且该设置器存在于原型中
你能给出[MCVE]演示你想要做什么? – jonrsharpe
我不明白为什么这会是一个问题。 'myPropVar'是一个匹配属性名称的字符串('lockMessage'),对吧?如果只是必须访问它,则不需要'@Input()'。你有错误信息吗? –
是的,我更新了我的帖子 – Velidan