加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular2/4 树形结构菜单示例

发布时间:2020-12-17 09:19:02 所属栏目:安全 来源:网络整理
导读:树型结构 组件 import { Component,Input } from '@angular/core';@Component({ selector: 'app-tree',templateUrl: './tree.component.html',styleUrls: ['./tree.component.scss']})export class TreeComponent { // 超简单,重点: 接收上级的值 // 可以为

树型结构

组件

import { Component,Input } from '@angular/core';

@Component({
  selector: 'app-tree',templateUrl: './tree.component.html',styleUrls: ['./tree.component.scss']
})
export class TreeComponent {

  // 超简单,重点: 接收上级的值
  // 可以为树建立一个接口,这里简化为any
  @Input() treelists: any

  // 点击动作
  itemClick(i) {
    // 建立一个服务来接收这个值,或者借助双向绑定,处理动作
    i._open = i._open  // 本例只简单演示开关,借助 treelists本身实现
    console.log(i)
  }
}

模板内容

<ul>
  <li *ngFor="let item of treelists">
    <button md-button (click)="itemClick(item)">{{item.title}}</button>
    <!--调用组件本身并 传值给下级: [treelists]="item.items"-->
    <app-tree *ngIf="item._open && item.items && item.items.length" [treelists]="item.items"></app-tree>
  </li>
</ul>

以上树组件完成,在其他组件中调用此组件即可

<app-tree [treelists]="menu"></app-tree>

数据/赋值

menu = [{
    title: '1',_open:true,//默认打开第一级
    items: [{
      title: '1.1',items: [
        {
          name: '1.1.1',title: 'xxx',items: []
        }
      ]
    },{
      title: '1.2',items:[]
    }
    ]
  }]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读