2015-04-22 05:51:41 -07:00
.statement
h4 Live Examples
p.
If you want to skip to the working examples you can check out these links on Plunker. <a href='http://plnkr.co/edit/lt7vPiJYwkHDKaTHGCUC?p=preview')>TypeScript Example</a> or <a href='http://plnkr.co/edit/CqquuEyUw2LgwY0IrXUZ?p=preview'> ES5 Example</a>.
2015-04-20 02:29:59 -07:00
.l-main-section
2015-04-22 05:51:41 -07:00
h2#section-its-all-a-tree It's all a tree
2015-04-20 02:29:59 -07:00
p.
As mentioned earlier, you build Angular applications as a tree of nested components. You've seen how to create
a top-level component. You add child components to a parent component by using them in the parent component's
template.
p.
Given a bootstrapping template with a <code><parent></code> tag in the body, you can create a parent
component that uses a <code><child></code> component like so:
pre.prettyprint.linenums.lang-javascript
code.
//ES5
function ParentComponent() {
this.message = "I'm the parent";
}
ParentComponent.annotations = [
new angular.Component({
selector: "parent"
}),
new angular.View({
template:
'<h1>{{ message }}</h1>' +
'<child></child>',
directives: [ChildComponent]
})
];
pre.prettyprint.linenums.lang-typescript
code.
//TypeScript
@Component({
selector: 'parent'
})
@View({
template: `
<h1>{{ message }}</h1>
<child></child>
`,
directives: [ChildComponent]
})
class ParentComponent {
message: string;
constructor() {
this.message = "I'm the parent";
}
}
p You then just need to write the <code>ChildComponent</code> class to make it work:
pre.prettyprint.linenums.lang-javascript
code.
//ES5
function ChildComponent() {
this.message = "I'm the child";
}
ChildComponent.annotations = [
new angular.Component({
selector: "child"
}),
new angular.View({
template: '<p> {{ message }} </p>'
})
];
pre.prettyprint.linenums.lang-typescript
code.
//TypeScript
@Component({
selector: 'child'
})
@View({
template: `
<p> {{ message }} </p>
`
})
class ChildComponent {
constructor() {
this.message = "I'm the child";
}
}
p.
Notice that in addition to using the <code><child></code> element in the parent template, you also need to
2015-04-22 05:55:25 -07:00
add <code>ChildComponent</code> in <code>ParentComponent</code>'s list of directives.