Skip to content

Commit 5b3d9b8

Browse files
committed
added example for passing data into child component
1 parent 6a48dda commit 5b3d9b8

File tree

9 files changed

+69
-70
lines changed

9 files changed

+69
-70
lines changed

app/ui-category/listview/listview-examples.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Link } from "./../../link";
44
let menuLinks = [
55
new Link("Create ListView", "/list-view/creating-list-view"),
66
new Link("Customize ListView", "/list-view/customizing-list-view"),
7-
new Link("Use item template", "/list-view/using-item-template"),
7+
new Link("Update Child Component", "/list-view/update-child-component"),
88
new Link("Use async pipe", "/list-view/using-async-pipe")
99
];
1010

app/ui-category/listview/listview-examples.module.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ListViewExamplesComponent } from "./listview-examples.component";
55
import { CreatingListViewComponent } from "./creating-listview/creating-listview.component";
66
import { CustomizingListViewComponent } from "./customizing-listview/customizing-listview.component";
77
import { UsingAsyncPipeComponent } from "./using-async-pipe/using-async-pipe.component";
8-
import { UsingItemTemplateComponent, ItemComponent } from "./using-item-template/using-item-template.component";
8+
import { UpdateChildComponent, ItemComponent } from "./update-child-component/update-child-component.component";
99
import { TitleAndNavButtonModule } from "../../directives/title-and-nav-button.module";
1010

1111
export const routerConfig = [
@@ -29,9 +29,9 @@ export const routerConfig = [
2929
data: { title: "Using async pipe" }
3030
},
3131
{
32-
path: "using-item-template",
33-
component: UsingItemTemplateComponent,
34-
data: { title: "Using item template" }
32+
path: "update-child-component",
33+
component: UpdateChildComponent,
34+
data: { title: "Update child component" }
3535
}
3636
];
3737

@@ -49,7 +49,7 @@ export const routerConfig = [
4949
CreatingListViewComponent,
5050
CustomizingListViewComponent,
5151
UsingAsyncPipeComponent,
52-
UsingItemTemplateComponent
52+
UpdateChildComponent
5353
]
5454
})
5555

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This example demonstrates how to use OnChanges to trigger updates in child components for data passed from parent component.
2+
The data is pased with `@Input` and the change is triggered via``ngOnCganges` with injecting `SimpleChnages` in the child's constructor.
3+
4+
TypeScript
5+
<snippet id='update-child-component-code'/>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.odd {
2+
vertical-align: center;
3+
padding-left: 15;
4+
}
5+
6+
.even {
7+
color: #0099CC;
8+
vertical-align: center;
9+
padding-left: 15;
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<StackLayout sdkExampleTitle sdkToggleNavButton>
2+
<GridLayout rows="auto, *">
3+
<ListView class="list-group" [items]="items" row="1">
4+
<template let-item="item" let-odd="odd" let-even="even" >
5+
<StackLayout [class.odd]="odd" [class.even]="even">
6+
<sdk-child-component [data]="item"></sdk-child-component>
7+
</StackLayout>
8+
</template>
9+
</ListView>
10+
</GridLayout>
11+
</StackLayout>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// >> update-child-component-code
2+
import { Component, ChangeDetectionStrategy, Input, OnChanges, SimpleChanges } from "@angular/core";
3+
4+
@Component({
5+
selector: "sdk-child-component",
6+
moduleId: module.id,
7+
template: `
8+
<StackLayout orientation="horizontal">
9+
<Label text="This is item with ID: " class="m-16" textWrap="true"></Label>
10+
<Label [text]="myData.id" textWrap="true"></Label>
11+
</StackLayout>
12+
`
13+
})
14+
export class ItemComponent implements OnChanges {
15+
@Input() data: any;
16+
public myData: any;
17+
18+
ngOnChanges(changes: SimpleChanges) {
19+
this.myData = changes["data"].currentValue;
20+
}
21+
}
22+
23+
@Component({
24+
styleUrls: ["./update-child-component.component.css"],
25+
moduleId: module.id,
26+
templateUrl: "./update-child-component.component.html"
27+
})
28+
export class UpdateChildComponent {
29+
public items: Array<any> = [];
30+
31+
constructor() {
32+
for (let index = 0; index < 20; index++) {
33+
this.items.push({ "id": index + 1 });
34+
}
35+
}
36+
}
37+
// << update-child-component-code

app/ui-category/listview/using-item-template/article.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

app/ui-category/listview/using-item-template/using-item-template.component.html

Lines changed: 0 additions & 10 deletions
This file was deleted.

app/ui-category/listview/using-item-template/using-item-template.component.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)