【发布时间】:2019-04-26 06:26:34
【问题描述】:
如果是这种情况,我可以使用以下语法将一些数据传递给孩子:
parent.component.ts
// imports..
selector: "app-parent",
templateURL: "./parent.component.html",
// ...
export class ParentComponent{
public statement= "I am your father"!;
}
parent.component.html
<div>
<h1>
My name is Darth Vader
</h1>
</div>
<app-child [luke] = "statement"></app-child> // child selector!
child.component.ts
// imports..
selector: "app-child",
templateURL: `<h2>{{"Luke, " + luke}}</h2>`
// ...
export class TestComponent{
@Input() public luke;
}
结果应该是:
卢克,我是你的父亲!
到目前为止没有什么特别的。
现在如果我的子组件包含另一个子组件并因此充当其父组件怎么办?
child.component.html
<app-secondChild></app-secondChild> // selector of second.child.component.ts!
second.child.component.html
<app-thirdChild></app-thirdChild> // selector of third.child.component.ts!
third.child.component.html
<app-fourthChild></app-fourthChild> // selector of fourth.child.component.ts!
基本上,我想将数据从父级传递给最后一个子级,以激活一些 HTML 代码。
我希望这没有解释得太复杂。
【问题讨论】:
标签: angular typescript