import { Component } from '@angular/core'; import { DataService } from './data.service'; @Component({ selector: 'app-root', template: ` <h2>Lazy Load</h2> <eui-tree [data]="data" (nodeExpand)="onNodeExpand($event)"></eui-tree> `, providers: [DataService] }) export class AppComponent { data: any[]; constructor(public dataService: DataService){} ngOnInit() { this.dataService.getNodes().subscribe((data) => this.data = data); } onNodeExpand(event){ let node = event; if (!node.children) { this.dataService.getNodes(node).subscribe((data) => node.children = data); } } }
import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { HttpModule } from '@angular/http'; import { EasyUIModule } from 'easyui/easyui/easyui.module'; import { AppComponent } from './app.component'; @NgModule({ bootstrap: [ AppComponent ], declarations: [ AppComponent ], imports: [ FormsModule, BrowserModule, BrowserAnimationsModule, HttpModule, EasyUIModule ] }) export class AppModule { }
import { Injectable } from '@angular/core'; import { Observable } from 'rxjs/Rx'; import 'rxjs/add/observable/of'; import 'rxjs/add/operator/delay'; @Injectable() export class DataService { nodes: any[] = [{ "id":1, "text":"My Documents", "children":[{ "id":11, "text":"Photos", "state":"closed" },{ "id":12, "text":"Program Files", "state":"closed" },{ "id":13, "text":"index.html" },{ "id":14, "text":"about.html" },{ "id":15, "text":"welcome.html" }] }]; photos: any[] = [{ "id":111, "text":"Friend" },{ "id":112, "text":"Wife" },{ "id":113, "text":"Company" }]; programs: any[] = [{ "id":121, "text":"Intel" },{ "id":122, "text":"Java" },{ "id":123, "text":"Microsoft Office" },{ "id":124, "text":"Games" }]; getNodes(node: any = null) { if (!node){ return Observable.of(this.nodes); } if (node.id == 11){ return Observable.of(this.photos).delay(1000); } else if (node.id == 12){ return Observable.of(this.programs).delay(1000); } } }
import { enableProdMode } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app.module'; enableProdMode(); platformBrowserDynamic().bootstrapModule(AppModule);