【问题标题】:To perform two way data binding in tree component在树组件中执行双向数据绑定
【发布时间】:2018-09-11 13:58:53
【问题描述】:

我在我的项目中使用 tree 组件。当我从 父元素 中选择 子元素(即 HTML5)时(即 Web 技术)。选定的子 (HTML5) 元素显示在输入字段中,如下图所示。

除此之外,我想为 child element 分配一些值,并在选择 child element (HTML) 的同时将其显示在其他 div 中。As如下图所示。

这里我需要执行两种数据绑定。我为 select 组件 获得了资源,但无法为 tree 组件 获取它。这是stackblitz 链接。

【问题讨论】:

  • 您能否详细说明您面临的问题是什么?
  • 这里选择/单击子元素(即 HTML5)。我需要在树组件之外显示一些数据(虚拟),只是为了表明我选择了 HTML5。

标签: angular angular-material


【解决方案1】:

DEMO

HTML:

<div class="text-inside">
    <mat-form-field>
        <input matInput placeholder="Select offering" [(ngModel)]="selectedItem" (focus)="showDropDown = true">
    </mat-form-field>
    <mat-tree [dataSource]="dataSource" [treeControl]="treeControl" *ngIf="showDropDown">
        <!-- Leaf node -->
        <mat-tree-node *matTreeNodeDef="let node" matTreeNodePadding (click)="selectedItem = node.item;showDropDown = false;">
            <button mat-icon-button disabled></button> {{node.item}}
        </mat-tree-node>

        <!-- expandable node -->
        <mat-tree-node *matTreeNodeDef="let node; when: hasChild" matTreeNodePadding>
            <button mat-icon-button [attr.aria-label]="'toggle ' + node.filename" (click)="loadChildren(node)" matTreeNodeToggle>
                <mat-icon class="mat-icon-rtl-mirror">
                  {{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
                </mat-icon>
              </button> {{node.item}}
        </mat-tree-node>

        <mat-tree-node *matTreeNodeDef="let node; when: isLoadMore">
            <button mat-button (click)="loadMore(node.loadMoreParentItem)">
                Load more...
              </button>
        </mat-tree-node>
    </mat-tree>
    <div [hidden]="!selectedItem || showDropDown">
        <div>
            List Of Guides {{selectedItem}}:
            <ul>
                <li *ngFor="let li of Guides">{{li}}</li>
            </ul>
        </div>
    </div>
</div>

编辑:

<div [hidden]="!selectedItem || showDropDown">
        <div>
            List Of Guides {{selectedItem}}:
            <ul>
                <li *ngFor="let li of getGuides(selectedItem)?.data">{{li}}</li>
            </ul>
        </div>
    </div>

TS:

Guides: Array<any> = [
    {name: 'css3' , data:  ['Tutorial Css3', 'Videos Css3' , 'Questions Css3']},
    {name: 'HTML5' , data :  ['Tutorial HTML5', 'Videos HTML5', 'Questions HTML5']},

    {name: 'Javascript', data:  ['Tutorial Javascript', 'Videos Javascript', 'Questions Javascript']},

  ]
  hideGuide: boolean = false;

  getGuides(selectedItem){
    return this.Guides.find(data => data.name == selectedItem )
  }

【讨论】:

  • 如何为另一个子元素分配不同的值,例如如果我选择 css 我想显示另一组值。
  • 使用键创建指南数组:指南:数组 = [ {'css': ['Tutorial', 'Videos', 'Questions']} ]
  • 我试过了,但输出是这样的[object Object]
  • 很抱歉,我无法提取对象。
  • 我知道了,非常感谢。
猜你喜欢
  • 2019-10-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-15
  • 2015-07-10
相关资源
最近更新 更多