Skip to content

Commit 7229224

Browse files
author
maksym.koretskyi
committed
Task: Moving a view between containers inside child components
1 parent e4f8475 commit 7229224

File tree

3 files changed

+2
-30
lines changed

3 files changed

+2
-30
lines changed

src/app/app.component.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,15 @@ import { TodoListComponent } from './todo-list.component';
1515
template: `
1616
<div class="top">
1717
<div>All tasks</div>
18-
<to-do-list></to-do-list>
1918
</div>
2019
<div class="bottom">
2120
<div>Completed tasks</div>
22-
<to-do-list></to-do-list>
2321
</div>
24-
<ng-template #tpl>
25-
<span>I am a task</span>
26-
</ng-template>
2722
<button (click)="move()">move</button>
2823
`,
2924
styles: ['.top, .bottom {border: 1px solid black; padding: 10px; margin: 10px 0; height: 30px; width: 500px; }']
3025
})
31-
export class AppComponent implements AfterViewInit {
32-
@ViewChildren(TodoListComponent) comps: QueryList<TodoListComponent>;
33-
@ViewChild('tpl') tpl: TemplateRef<null>;
34-
embeddedView: EmbeddedViewRef<null>;
35-
isTop: boolean = true;
36-
37-
ngAfterViewInit() {
38-
this.embeddedView = this.tpl.createEmbeddedView(null);
39-
this.comps.first.addItem(this.embeddedView);
40-
this.isTop = true;
41-
}
42-
26+
export class AppComponent {
4327
move() {
44-
if (!this.isTop) {
45-
this.comps.last.removeItem(0);
46-
this.comps.first.addItem(this.embeddedView);
47-
this.isTop = true;
48-
} else {
49-
this.comps.first.removeItem(0);
50-
this.comps.last.addItem(this.embeddedView);
51-
this.isTop = false;
52-
}
5328
}
5429
}

src/app/todo-list.component.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, ViewChild, ViewContainerRef } from '@angular/core';
1+
import { Component } from '@angular/core';
22

33
@Component({
44
selector: 'to-do-list',
@@ -7,13 +7,10 @@ import { Component, ViewChild, ViewContainerRef } from '@angular/core';
77
`,
88
})
99
export class TodoListComponent {
10-
@ViewChild('vc', {read: ViewContainerRef}) vc: ViewContainerRef;
1110

1211
addItem(v) {
13-
this.vc.insert(v);
1412
}
1513

1614
removeItem(index) {
17-
return this.vc.detach(index);
1815
}
1916
}

task.jpg

-28.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)