gpt4 book ai didi

angular - *ngIf 在使用 observable 时在 Angular Material 表中不起作用

转载 作者:太空狗 更新时间:2023-10-29 18:35:26 38 4
gpt4 key购买 nike

我正在尝试使用具有可扩展行的 Angular Material 表,以便仅在屏幕宽度低于特定断点时显示信息。我正在使用我在 HTML 中订阅的可观察的 isHandSetPortrait$ 来确定是否已到达断点。

该表有两列(名称信息)。 Name无论isHandsetPortrait为true还是false都显示,Info仅在isHandsetPortrait$时显示错误。这按预期工作。

在可扩展行中,我有一个 *ngIf,它使用相同的可观察对象 isHandsetPortrait$,在 isHandsetPortrait$ 的输出时显示文本> 为 true。但是,这似乎不起作用。

table-expandable-rows-example.html

<!-- *ngIf works here -->
<div *ngIf="isHandsetPortrait$ | async">ngIf works here</div>
<div class="mat-elevation-z8">
<table mat-table [dataSource]="dataSource" matSort matSortActive="priority" matSortDirection="desc"
multiTemplateDataRows>


<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Name</th>
<td mat-cell *matCellDef="let row"> {{row.name}}</td>
</ng-container>

<!-- Info Column -->
<ng-container matColumnDef="info" >
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let row" class="info-cell">
Info
</td>
</ng-container>

<!-- Expanded Content Column - The detail row is made up of this one column that spans across all columns -->
<ng-container matColumnDef="expandedDetail">
<td mat-cell *matCellDef="let element" [attr.colspan]="displayedColumns.length">
<div class="expanded-content" [@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
Additional text....
<!-- *ngIf does not work here -->
<div *ngIf="isHandsetPortrait$ | async">ngIf does not work here</div>
</div>
</td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="getDisplayedColumns(isHandsetPortrait$ | async)"></tr>
<tr mat-row *matRowDef="let element; columns: getDisplayedColumns(isHandsetPortrait$ | async);"
class="example-element-row" [class.example-expanded-row]="expandedElement === element"
(click)="expandedElement = expandedElement === element ? null : element"></tr>
<tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
</table>
</div>

table-expandable-rows-example.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';
import { animate, state, style, transition, trigger } from '@angular/animations';
import { from, Observable, of, pipe } from 'rxjs';
import { concatMap, delay, map, share } from 'rxjs/operators';
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';

/**
* @title Table with expandable rows
*/
@Component({
selector: 'table-expandable-rows-example',
styleUrls: ['table-expandable-rows-example.css'],
templateUrl: 'table-expandable-rows-example.html',
animations: [
trigger('detailExpand', [
state('collapsed', style({ height: '0px', minHeight: '0' })),
state('expanded', style({ height: '*' })),
transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)'))
]),
]
})
export class TableExpandableRowsExample {

public isHandsetPortrait$: Observable<boolean> = this.breakpointObserver.observe(Breakpoints.HandsetPortrait)
.pipe(
map(orientationState => orientationState.matches),
share()
);

private chatRequests = [
{
name: 'John Wayne'
},
{
name: 'Oscar Wilde'
},
{
name: 'Eric Cantona'
}
];

dataSource = new MatTableDataSource(this.chatRequests);

displayedColumns: { def: string, isMobile: boolean }[] = [
{
def: 'name',
isMobile: true
},
{
def: 'info',
isMobile: false
}
];

constructor(private breakpointObserver: BreakpointObserver) { }


public getDisplayedColumns(isHandsetPortrait: boolean) {
return this.displayedColumns
.filter(cd => !isHandsetPortrait || cd.isMobile)
.map(cd => cd.def);
}

}

我整理了一个 StackBlitz project这应该突出我遇到的问题。

*ngIf 在表外工作(html 模板的第 2 行),但在表内不起作用(html 的第 28 行) 模板)。我怎样才能使第二个 *ngIf 工作?

最佳答案

你必须使用 shareReplay 来让它工作,使用 share 对第二个订阅者来说什么都不会发生

public isHandsetPortrait$: Observable<boolean> = this.breakpointObserver.observe(Breakpoints.HandsetPortrait)
.pipe(
map(orientationState => orientationState.matches),
shareReplay(1) // here
);

https://stackblitz.com/edit/angular-ml4vym-71mba5

关于angular - *ngIf 在使用 observable 时在 Angular Material 表中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55950761/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com