小编Jul*_*lfs的帖子

如何将@Input()参数传递给使用DynamicComponentLoader创建的角度2组件

DynamicContentLoader文档没有解释我如何正确加载子组件的输入.假设我有一个孩子喜欢:

@Component({
  selector: 'child-component',
  template: '<input type="text" [(ngModel)]="thing.Name" />'
})
class ChildComponent {
  @Input() thing : any;
}
Run Code Online (Sandbox Code Playgroud)

和父母一样:

@Component({
  selector: 'my-app',
  template: 'Parent (<div #child></div>)'
})
class MyApp {
  thing : any;
  constructor(dcl: DynamicComponentLoader, elementRef: ElementRef) {
    dcl.loadIntoLocation(ChildComponent, elementRef, 'child');
  }
}
Run Code Online (Sandbox Code Playgroud)

我应该如何thing进入子组件,以便两个组件可以绑定同一个数据.

我试着这样做:

@Component({
  selector: 'my-app',
  template: 'Parent (<div #child></div>)'
})
class MyApp {
  thing : any;
  constructor(dcl: DynamicComponentLoader, elementRef: ElementRef) {
    dcl.loadIntoLocation(ChildComponent, elementRef, 'child').then(ref => {
        ref.instance.thing = this.thing;
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

它有点工作,但它们没有像你期望的那样同步.

基本上我试图通过在角度1中使用ng-include来实现同样的事情,其中​​子项是动态确定的组件并与其父项共享模型.

提前致谢 …

angular

11
推荐指数
1
解决办法
4218
查看次数

标签 统计

angular ×1