gpt4 book ai didi

垫子图标按钮和垫子按钮之间的 Angular Material 切换

转载 作者:行者123 更新时间:2023-12-04 16:04:54 33 4
gpt4 key购买 nike

根据媒体查询,有没有更漂亮的方法可以在 mat-icon-button 和 mat-button 之间切换?我已经对我当前的解决方案做了一个演示,但它需要两个独立的按钮。

<button type=button mat-icon-button fxHide fxShow.lt-sm (click)="onEdit($event)">
<mat-icon>edit</mat-icon>
</button>
<button type="button" mat-button fxHide.lt-sm (click)="onEdit($event)">
<mat-icon>edit</mat-icon> Edit
</button>

DEMO

最佳答案

遇到同样的问题,目前正在使用此解决方案。

组件.html:

<button
mat-button
[ngClass]="isBigDevice() ? 'mat-stroked-button' : 'mat-icon-button'"
color="accent"
type="button"
(click)="coolMethodToDoStuff()"
[disabled]="isSelected() && contextTypeIsSomething()">
<span *ngIf="isBigDevice()">Edit</span>
<mat-icon *ngIf="!isBigDevice()">edit</mat-icon>
</button>

有问题/注释:需要添加基本按钮样式/属性 mat-button .如果你不这样做,你会得到一个错误 color对这种 html 元素没有属性。优点是,如果您在一个按钮上有多个属性或 disabled 的条件, hidden*ngIf你不需要它们两次。我知道 *ngIf缺乏可读性,但目前它是避免大量重复代码的最佳方法。

我的下一步:我将为这样的事情建立一个指令。

编辑:

更多的是我的 MediaQueryService:
import {Injectable, OnDestroy} from '@angular/core';
import {BreakpointObserver, Breakpoints, BreakpointState} from '@angular/cdk/layout';
import {Subject} from 'rxjs';
import {takeUntil} from 'rxjs/operators';

@Injectable({
providedIn: 'root'
})

export class MediaQueryService implements OnDestroy {

private _breakpointArray = {
isXSmall: false,
isSmall: false,
isMedium: false,
isLarge: false,
isXLarge: false,
};
private _destroy$ = new Subject();

constructor(
private _breakpointObserver: BreakpointObserver,
) {
this._breakpointObserver
.observe([
Breakpoints.WebLandscape,
Breakpoints.WebPortrait,
])
.pipe(
takeUntil(this._destroy$)
)
.subscribe((state: BreakpointState) => {
_breakpointObserver.isMatched(Breakpoints.WebLandscape)
? this.webOrientationChangeLogger('landscape')
: this.webOrientationChangeLogger('portrait');
});

this._breakpointObserver.observe([
Breakpoints.XSmall,
Breakpoints.Small,
Breakpoints.Medium,
Breakpoints.Large,
Breakpoints.XLarge,
]
)
.pipe(
takeUntil(this._destroy$)
)
.subscribe((state: BreakpointState) => {
this._breakpointArray.isXSmall = _breakpointObserver.isMatched(Breakpoints.XSmall);
this._breakpointArray.isSmall = _breakpointObserver.isMatched(Breakpoints.Small);
this._breakpointArray.isMedium = _breakpointObserver.isMatched(Breakpoints.Medium);
this._breakpointArray.isLarge = _breakpointObserver.isMatched(Breakpoints.Large);
this._breakpointArray.isXLarge = _breakpointObserver.isMatched(Breakpoints.XLarge);
});
}

public ngOnDestroy(): void {
this._destroy$.next();
this._destroy$.complete();
}

public get isSmallDevice(): boolean {
return this.getBreakpointBoolean('isXSmall') || this.getBreakpointBoolean('isSmall');
}

public get isBigDevice(): boolean {
return this.getBreakpointBoolean('isXLarge') || this.getBreakpointBoolean('isLarge');
}

public getBreakpointBoolean(breakpointName: string): boolean {
return this._breakpointArray[breakpointName];
}

private webOrientationChangeLogger(orientation: string) {
console.log(orientation);
}

}

在我使用按钮的组件内,我只是调用 isBigDevice()方法并返回 bool 值。

组件.ts:
public isBigDevice(): boolean {
return this._mediaQueryService.isBigDevice();
}

您也可以尝试使用此 fxShow , fxHide Angular Material 的特性。

关于垫子图标按钮和垫子按钮之间的 Angular Material 切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48899593/

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