如何使用angular 4编写Onload函数?

时间:2018-01-01 11:07:42

标签: angular typescript spring-boot

我正在使用角4复选框树结构,我想知道
如何在角度4中写入onload函数?

ang.component.ts

这是我的树视图组件

import { Component, ViewChild } from '@angular/core';
import { TreeViewComponent } from '@syncfusion/ej2-ng-navigations';
@Component({
    selector: 'app-checkboxes',   
    templateUrl: './checkboxes.component.html'
})
export class CheckboxesComponent {
    //@ViewChild('samples')
    constructor() {
    }  
    public countries: Object[] = [
        { id: 1, name: 'Australia', hasChild: true, expanded: true },
        { id: 2, pid: 1, name: 'New South Wales', isChecked: true },
        { id: 3, pid: 1, name: 'Victoria' },
        { id: 4, pid: 1, name: 'South Australia' },
        { id: 6, pid: 1, name: 'Western Australia', isChecked: true },
        { id: 7, name: 'Brazil', hasChild: true },
        { id: 8, pid: 7, name: 'Paraná' },
        { id: 9, pid: 7, name: 'Ceará' },
        { id: 10, pid: 7, name: 'Acre' },
        { id: 11, name: 'China', hasChild: true },
        { id: 12, pid: 11, name: 'Guangzhou' },
        { id: 13, pid: 11, name: 'Shanghai' },
        { id: 14, pid: 11, name: 'Beijing' },
        { id: 15, pid: 11, name: 'Shantou' },
        { id: 16, name: 'France', hasChild: true },
        { id: 17, pid: 16, name: 'Pays de la Loire' },
        { id: 18, pid: 16, name: 'Aquitaine' },
        { id: 19, pid: 16, name: 'Brittany' },
        { id: 20, pid: 16, name: 'Lorraine' },
        { id: 21, name: 'India', hasChild: true },
        { id: 22, pid: 21, name: 'Assam' },
        { id: 23, pid: 21, name: 'Bihar' },
        { id: 24, pid: 21, name: 'Tamil Nadu' },
        { id: 25, pid: 21, name: 'Punjab' }
    ];
    public field: Object = {
        dataSource: this.countries,
        id: 'id',
        parentID: 'pid',
        text: 'name',
        hasChildren: 'hasChild'
    };
    public showCheckBox: boolean = true;
    public checkedNodes: string[] = ['2', '6'];
}

ang.component.html: 这是我的树视图结构,使用复选框

 <p>Angular 4 Checkbox Tree</p>
    <div id='treeparent'>
        <ej-treeview id='treeelement' [fields]='field'
            [showCheckBox]='showCheckBox'> </ej-treeview>
    </div>

1 个答案:

答案 0 :(得分:1)

如果我理解正确,你正在寻找通常用于在视图加载之前设置东西的ngOnInit。您可以阅读更多相关信息here

简单的例子:

export class Example implements OnInit{
 constructor(){}

 ngOnInit(){
 //code that will execute at the start of the loading process
 }
}