Tot*_*ati 6 responsive-design angular-material angular-flex-layout angular
根据媒体查询,是否有任何更漂亮的方式在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>
Run Code Online (Sandbox Code Playgroud)
遇到同样的问题,目前正在使用此解决方案。
组件.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>
Run Code Online (Sandbox Code Playgroud)
有一个问题/注释:您需要添加基本按钮样式/属性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);
}
Run Code Online (Sandbox Code Playgroud)
}
在我使用按钮的组件内,我只需调用该isBigDevice()方法并返回布尔值。
组件.ts:
public isBigDevice(): boolean {
return this._mediaQueryService.isBigDevice();
}
Run Code Online (Sandbox Code Playgroud)
您也可以尝试使用角材料的fxShow属性fxHide。
| 归档时间: |
|
| 查看次数: |
855 次 |
| 最近记录: |