我一直在寻找一个解决方案。我已经试过了一堆从@input,@Query,dynamicContentLoader,@注入,@Inject(forwardRef(()不同的事情,但一直没能还没有想出解决办法Angular 2,如何将选项从父组件传递给子组件?
我的例子结构:
parent.ts
import {Component} from 'angular2/core';
@Component({
selector : 'my-app',
directives : [Child],
template : `<parent-component></parent-component>`
})
export class Parent
{
private options:any;
constructor()
{
this.options = {parentThing:true};
}
}
child.ts
import {Component} from 'angular2/core';
@Component({
selector : 'parent-component',
template : `<child-component></child-component>`
})
export class Child
{
constructor(private options:any) <- maybe I can pass them in here somehow?
{
// what I am trying to do is get options from
// the parent component at this point
// but I am not sure how to do this with Angular 2
console.log(options) <- this should come from the parent
// how can I get parent options here?
// {parentThing:true}
}
}
这是DOM我目前的HTML输出,所以这部分工作正常
<parent-component>
<child-component></child-component>
</parent-component>
问题概括为:
如何传递从父组件选项的子组件和有那些在子构造函数中可用的选项?
简单绑定有什么问题?子中的@Input()'和
我已经看到过很多次。它很可能是解决方案。无论出于什么原因,我都不知道问题是如何结合在一起的。我在@Input中混淆了一段时间..但是我认为我很难将所有隐含的东西包裹起来,以及如何使它正常工作。 –
基本上我已经尝试了所有这些..'@Input,@Query,dynamicContentLoader,@Inject,@Inject(forwardRef(()'但我一直无法让他们做我想做的事情。我不是说他们做不到,但或多或少,我最大限度地减少了我的问题,以表明我试图达到什么样的最终结果。其中之一可能是解决方案。 –