angular - 使用角度设备检测器服务隐藏平板电脑的按钮
问题描述
我正在尝试仅使用角度 ngx 设备检测器服务在平板电脑上隐藏一个元素。
我已经使用 css 媒体查询完成了这项工作。但是,我想看看是否可以使用 ngx 设备检测器获得相同的结果。
html
<div id="hide-download">
<app-button name="download" classAttr="btn-primary" (click)="downloadFile()" [disabled]="!allOrders || allOrders.length === 0">
<i class="fas fa-download button-icon-padding"></i>
Download
</app-button>
Angular 服务 ts
@Injectable()
export class ExampleDeviceDetectorService {
public deviceInfo: any;
public isMobile: any;
public isTablet: any;
public isDesktop: any;
constructor(
public deviceService: DeviceDetectorService
) {
this.getDeviceInfo();
}
getDeviceInfo() {
this.deviceInfo = this.deviceService.getDeviceInfo();
this.isMobile = this.deviceService.isMobile();
this.isTablet = this.deviceService.isTablet();
this.isDesktop = this.deviceService.isDesktop();
}
}
我预计该按钮不会显示在平板电脑设备上。
解决方案
将服务注入到您的组件中
样本.component.ts
constructor(private exampleDeviceDetectorService: ExampleDeviceDetectorService) {}
使用 *ngIf 从 DOM 中删除元素
示例.component.html
<div id="hide-download" *ngIf="!exampleDeviceDetectorService.isTablet">
<app-button name="download" classAttr="btn-primary"
(click)="downloadFile()" [disabled]="!allOrders || allOrders.length === 0">
<i class="fas fa-download button-icon-padding"></i>
Download
</app-button>
</div>
使用 [hidden] 从 DOM 中隐藏元素
示例.component.html
<div id="hide-download" [hidden]="!exampleDeviceDetectorService.isTablet">
<app-button name="download" classAttr="btn-primary"
(click)="downloadFile()" [disabled]="!allOrders || allOrders.length === 0">
<i class="fas fa-download button-icon-padding"></i>
Download
</app-button>
</div>
推荐阅读
- python - 如何让 Instagram 关注者列表向下滚动并继续关注?
- javascript - THREE.JS - 两张脸的交点处的毛刺线
- python - 将前导零添加到日期时间列的分钟部分
- javascript - 为什么 Typescript 抱怨一个对象不能分配给它的类型?
- sql - 循环假脱机脚本
- javascript - 如何使用 JavaScript 从表数据计算正常运行时间
- python - Scrapy Xpath 在同一循环中提取 h6 和 ul/li 选择器
- html - CSS 最大宽度百分比,元素长度
- sql - CASE WHEN 中的 ROUND 功能不起作用
- mysql - Mysql:如何根据具有多对多关系的另一个表更新表列