【问题标题】:how to write Onload function using angular 4?如何使用 Angular 4 编写 Onload 函数?
【发布时间】:2018-06-11 11:04:12
【问题描述】:

我正在使用 Angular 4 复选框树结构,我想知道
如何在 Angular 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>

【问题讨论】:

    标签: angular typescript spring-boot


    【解决方案1】:

    如果我理解正确,您正在寻找 ngOnInit,它通常用于在视图加载之前进行设置。你可以阅读更多关于它here

    简单示例:

    export class Example implements OnInit{
     constructor(){}
    
     ngOnInit(){
     //code that will execute at the start of the loading process
     }
    }
    

    【讨论】:

    • 还有一个疑问,先生如何将 id angular 4 传递给 spring boot 控制器?
    • 我假设你有这样的东西。http.get(url) 或 .post(url),通常你只需在函数中传递 id 并将其连接到 url getSpecific(id) { this.http.get(url + id) .. }
    • 好的先生....@akirus 如何在 Angular 4 中使用 Spring Boot 获取数据库列 ID?
    • 我不确定您的意思,但我建议您查看angular.io/guide/http 以更好地了解如何与您的 spring api 进行通信。
    猜你喜欢
    • 2019-04-16
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多