【问题标题】:To display 1st list-item value from mat-selection-list in another component by default默认情况下在另一个组件中显示来自 mat-selection-list 的第一个列表项值
【发布时间】:2019-04-17 04:37:09
【问题描述】:

我有 2 个组件称为:

1)list(用于显示客户列表)

2)detail(用于显示客户详细信息)

这两个组件是通用组件,所以我在另一个名为 customer 的组件中重新使用这些组件。将显示如下图所示:

在这里单击list 组件上的特定客户名称(前客户一),我在details 组件(即客户详细信息)上显示客户值(例如姓名和年龄)。这个场景工作正常。

但我希望第一个列表项(即客户)被突出显示,其值(即姓名和年龄)默认显示在详细信息组件上。 像这样:

Stackblitz链接

【问题讨论】:

    标签: angular typescript angular6 angular-components


    【解决方案1】:

    您可以在 ListComponent 加载后发出第一个值。

    ts

    export class ListComponent {
    
      ngOnInit() {
        if (this.contacts && this.contacts.length > 0) {
          this.select.emit(this.contacts[0]);  //<-- emit the first value
        }
    
      }
    

    html

      <mat-selection-list  #contact>
            <mat-list-option 
          [ngClass]="{selected : contact.name == currentContact.name}"  
          *ngFor="let contact of contacts">
                <a mat-list-item (click)="onSelect(contact)"><span>{{ contact.name }}</span> </a>
            </mat-list-option>
          </mat-selection-list>
    

    这是工作副本 - https://stackblitz.com/edit/list-examples-mine-mgshnt

    【讨论】:

    • 谢谢解答,能否突出显示第一个客户?
    • 什么类和你想放在哪里突出显示?
    • 在列表组件中,表示first list-item (i,e customer-one)默认为selected/highlighted..
    • 检查更新的答案和链接。我在mat-list-option 中添加了selected 类,但您可以根据需要进行更改。
    • 我看到了stackblitz链接,它没有突出显示第一个客户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 2013-09-23
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多