动态组件属性Angular 2的Setter

时间:2016-11-15 16:04:25

标签: angular typescript

我正在使用动态组件创建(resolveComponentFactory) 所以它适用于静态@Input()属性。但是对于动态设置器,它不起作用。

我无法做到this.componentReference.instance[myPropVar]= someValue 使用在组件内部创建的setter。

有可能吗?谢谢!

我的动态组件中的

属性设置器是:

 @Input() set lockMessage(val: any) {
    if (val) {
      console.log("Visit details -> ", val);

    }
  }

就像那篇文章一样 Angular 2 dynamic component creation

但是我想添加一些具有setter的属性到我动态创建的组件。

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);

    }

并抛出错误,因为该属性不存在。我检查了组件实例,并且原型

中存在setter

0 个答案:

没有答案