angular - 如何使用 Angular 材料中的按钮创建选择
问题描述
我有一个使用 googlemap 的页面,并在地图上放置了一些按钮,看起来像这样以供设计参考。
这是一个设计示例
我想要的是让这些按钮像一个单选按钮一样,这意味着如果用户点击一个按钮,我想改变背景颜色的外观。做这个的最好方式是什么 ?有没有办法修改材料选择,或者我需要通过 CSS 执行此操作并根据点击更改图像?
解决方案
由于没有明确的解决方案,我实际上只是使用基本的角材料和按钮来完成。在我的 HTML 中,我使用了以下代码
<button mat-mini-fab [ngClass]="[bntStyle1]" (click)="changeButton(1)">
<mat-icon style="font-size: 24px" fontSet="fa" fontIcon="fa-draw-polygon"></mat icon>
</button>
<button mat-mini-fab [ngClass]="[bntStyle2]" (click)="changeButton(2)">
<mat-icon>search</mat-icon>
</button>
<button mat-mini-fab [ngClass]="[bntStyle3]" (click)="changeButton(3)">
<mat-icon>search</mat-icon>
</button>
<button mat-mini-fab [ngClass]="[bntStyle4]" (click)="changeButton(4)">
<mat-icon>search</mat-icon>
</button>
然后在我的comonent中,我有一个名为change button的函数,它将根据单击的按钮设置样式。同样在 ngOnInit 上,我在第一次单击事件之前将它们设置为相同。我确信我可以清理它,但现在它可以完成工作并且就像一个选项/选择。
ngOnInit() {
this. bntStyle1 = 'btn-default';
this. bntStyle2 = 'btn-default';
this. bntStyle3 = 'btn-default';
this. bntStyle4 = 'btn-default';
}
changeButton(bttn) {
if (bttn === 1 ) {
this.bntStyle1 = 'btn-change';
this.bntStyle2 = 'btn-default';
this.bntStyle3 = 'btn-default';
this.bntStyle4 = 'btn-default';
this.drawingManager.setDrawingMode(google.maps.drawing.OverlayType.POLYGON);
}
if (bttn === 2 ) {
this.bntStyle1 = 'btn-default';
this.bntStyle2 = 'btn-change';
this.bntStyle3 = 'btn-default';
this.bntStyle4 = 'btn-default';
}
if (bttn === 3 ) {
this.bntStyle1 = 'btn-default';
this.bntStyle2 = 'btn-default';
this.bntStyle3 = 'btn-change';
this.bntStyle4 = 'btn-default';
}
if (bttn === 4 ) {
this.bntStyle1 = 'btn-default';
this.bntStyle2 = 'btn-default';
this.bntStyle3 = 'btn-default';
this.bntStyle4 = 'btn-change';
}
console.log('We hit button change');
}
推荐阅读
- javascript - DataTables - 如何让 columnDefs 宽度工作?
- amazon-ec2 - 无法连接到 EC2 上运行的 Airflow 服务器
- javascript - 无法采取行动时隐藏/显示浏览器历史记录返回按钮
- javascript - 如何在 Go 中重新创建这个 Javascript 数组结构?
- java - Java无法将数据插入Mongo
- python - 如何使用 Beautiful Soup 抓取 UL 类标签
- r - 在 R 中聚合来自 2 DF 的值
- ios - Cocoapods如何在工作区中将多个项目分组在同一组下
- listview - win32 列表视图(公共控件 6)上的 LVM_ENABLEGROUPVIEW 返回 0
- html - 改变背景颜色没有效果