【问题标题】:How to pass an object from parent component to child component which is created using DCL loadintolocation()?如何将对象从父组件传递到使用 DCL loadintolocation() 创建的子组件?
【发布时间】:2016-02-08 05:27:53
【问题描述】:

我正在使用 DCL 加载一个组件。我想将一个对象传递给该组件。我制作了一个 plunker 演示 http://plnkr.co/edit/QKlMNnjfrt8PyVnGdJG4?p=preview 其中 AppComponent 是父级,这是我要传递的对象 information = {name:"Abhi", place:"Banglore"};

export class SomeComponent {
public childform: ControlGroup;
constructor(fbs: FormBuilder) {  
this.childform = fbs.group({  
  'name':  ['', Validators.required],
  'place':['']
});
}

}

这是子组件...当新组件加载时,我想显示在子组件的输入框中传递的对象。我不知道如何在 DCL 中将数据从父级传递给子级...有人请帮帮我……

【问题讨论】:

    标签: angular


    【解决方案1】:

    ComponentRef 通过它的instance getter 提供对创建组件的引用

    loadIntoLocation(...).then(ref => {
      ref.instance.someField = someValue;
    } 
    

    另请参阅此类似问题How to call an event in parent component from child component which is loaded using DCL loadintolocation()

    【讨论】:

    • 当我尝试获取 ref.instance.items.results 的实例时,它在我的控制台窗口中显示为 undefined
    • 这是来自FakeComponent 对吗? @QueryList 仅在 ngAfterViewInit() AFAIK 之后填写。您可以将EventEmitter 添加到FakeComponent,以在执行ngAfterViewInit() 时发送事件。然后你可以等ref.instance.afterViewInitCalled.subscribe(value => { ref.instance.items.result...});
    猜你喜欢
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 2018-01-01
    • 2016-12-16
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    相关资源
    最近更新 更多