@Component({
selector: 'todo-list',
template: `
<p *ngFor='let item of todos'>{{ item.name }}</p>
`,
})
export class TodoComponent{
@Input() todos: Todo[];
public getTodos():Todo[]{
return this.todos;
}
}
@Component({
selector: 'app-todo-data',
template: `<p *ngFor='let item of todos'>{{ item.name }}</p>
<button (click)='getData()'>get data</button>`,
styleUrls: ['./todo-data.component.css'],
inputs: ['todoComponent'],
})
export class TodoDataComponent implements OnInit {
todoComponent: TodoComponent;
todos: Todo[]
constructor() { }
ngOnInit() {
}
getData(){
this.todos=this.todoComponent.getTodos();
}
}
//app.component.htm
<div>
<div *ngIf='loading'>
<todo-list [todos]='todos'></todo-list>
<button (click)='changeall()'>next</button>
</div>
</div>
<div>
<app-todo-data [todoComponent]='todoComponent'></app-todo-data>
</div>
//app.component.ts
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
todos: Todo[];
@ViewChild(TodoComponent)
todoComponent: TodoComponent;
loading: boolean = true;
constructor(private todoService:TodoService){
super(true);
}
ngOnInit(){
this.todoService.todos.subscribe(data => {this.todos=data});
this.todoService.load(0, 3);
}
changeall(){
this.todoService.load(3, 3);
}
}
constructor(private todoService:TodoService, private cdr: ChangeDetectorRef){}
ngAfterViewInit(){
this.cdr.detectChanges();
}
setInterval(() => {
this.numberOfTicks ++
// the following is required, otherwise the view will not be updated
this.ref.markForCheck();
}, 1000);
ref.detach();
setInterval(() => {
this.ref.detectChanges();
}, 5000);
@Component({
selector: 'todo-list',
changeDetection: ChangeDetectionStrategy.OnPush,
})
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有