【问题标题】:Angular 2 dynamic two-way bindingAngular 2 动态双向绑定
【发布时间】:2017-02-18 11:40:06
【问题描述】:

我正在尝试构建一个动态附加另一个组件的组件。例如,这里是我的父类:

import { Component, ComponentRef, ViewChild, ViewContainerRef, ComponentFactoryResolver } from '@angular/core';

@Component({
    templateUrl: './app/sample-component.component.html',
    selector: 'sample-component'
})
export class SampleComponent {

    @ViewChild('dynamicContent', { read: ViewContainerRef })
    protected dynamicComponentTarget: ViewContainerRef;
    private currentComponent: ComponentRef<any>;
    private selectedValue: any;

    constructor(private componentResolver: ComponentFactoryResolver) {

    }

    private appendComponent(componentType: any) {
        var factory = this.componentResolver.resolveComponentFactory(componentType);
        this.currentComponent = this.dynamicComponentTarget.createComponent(factory);
    }
}

sample-component.component.html:

<div #dynamicContent></div>

它可以很好地附加一个元素,但我不知道如何动态绑定双向,就像我在静态组件中所做的那样:[(ngModel)]="selectedValue"

【问题讨论】:

    标签: angular angular-ngmodel


    【解决方案1】:

    这是一种解决方法。

    来自上面发布的代码。

    private appendComponent(componentType: any) {
     var factory = this.componentResolver.resolveComponentFactory(componentType);
     this.currentComponent = this.dynamicComponentTarget.createComponent(factory);
     this.currentComponent.instance.value = this.selectedValue;
     this.currentComponent.instance.valueChange.subscribe(val => this.selectedValue = val);
    

    }

    将动态变量作为对象,现在将其分配给父组件中的变量。

    //at dynamically created component
    export class DynamicComponent{
    public bindedValue : Object = {
     value:''
     }
    }
    
    // at dynamic component template
    <input type="text" [(ngModel)]="bindedValue.value"/>
    
    
    //At parent component
    this.currentComponent.instance.bindedValue= this.selectedValue;
    

    现在 bindValue 和 selectedValue 将具有相同的对象引用。两者都将保持相同的值。

    【讨论】:

      【解决方案2】:

      不支持与动态添加的组件绑定。

      您可以使用共享服务与动态添加的组件进行通信 (https://angular.io/docs/ts/latest/cookbook/component-communication.html)
      或者您可以使用this.currentComponent 参考来强制读取/设置:

      private appendComponent(componentType: any) {
          var factory = this.componentResolver.resolveComponentFactory(componentType);
          this.currentComponent = this.dynamicComponentTarget.createComponent(factory);
          this.currentComponent.instance.value = this.selectedValue;
          this.currentComponent.instance.valueChange.subscribe(val => this.selectedValue = val);
      }
      

      【讨论】:

      • 好的,那么我认为使用ControlValueAccessor 接口会更好,但我不确定是否有任何组件提供像[(ngModel)]="selectedValue" 这样的构造来实现这个接口。所以它可以这样使用this.currentComponent.instance.registerOnChange(this.valueChangeHandler);
      • 不确定你的意思。 ControlValueAccessor 需要实现 [(ngModel)]="..." 才能工作,但这也是 Angular2 绑定,因此不支持动态添加的组件。
      • 能否请您扩展此@GünterZöchbauer ? this.currentComponent.instance.value 如何与 componentTypes 模板中的 DOM 输入元素相关联:` ... ` 以替换 ?如果您能分享一个 plunker,我将不胜感激。
      • @BenjaminMcFerren 它需要类似于&lt;input [(ngModel)]="value" ...&gt; 我认为您需要手动调用更改检测。要么使用this.currentInstance...(不记得细节),要么将this.cdRef:ChangeDetectorRef注入动态添加的组件并调用this.currentComponent.instance.cdRef.detectChanges()
      猜你喜欢
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 2016-12-29
      • 2017-02-11
      • 2017-03-17
      相关资源
      最近更新 更多