简体   繁体   English

Angular 材质复选框 alignment

[英]Angular Material checkbox alignment

I have a little alignment problem that I cannot solved.我有一个无法解决的小 alignment 问题。 I am using Angular Material checkbox component for showing list of options and the user can click on them and choose multiple options.我正在使用 Angular Material 复选框组件来显示选项列表,用户可以单击它们并选择多个选项。 Everything is working fine but i am facing a little bit of style issue that i really cannot solve.一切正常,但我面临着一些我真的无法解决的风格问题。

Code Sample on StackBlitz StackBlitz 上的代码示例

What I want to have at the end is have all the checkboxes aligned at the right of the available space.最后我想要的是将所有复选框对齐在可用空间的右侧。

我需要如何对齐的示例图像

Does anyone have a solution to this?有人对此有解决方案吗?

Adding this to your css will work:将此添加到您的 css 将起作用:

::ng-deep .mat-checkbox-inner-container {
  margin-right: 0 !important;

} }

You need to either use.important or increase your selector specificity by adding an id on the checkbox and using it in the selector.您需要使用.important 或通过在复选框上添加一个 id 并在选择器中使用它来增加您的选择器特异性。

An easy way to solve this is to use Angular Material's mat-selection-list ( Angular Material list API reference docs ) and then excluding its ripple effect, where the checkbox is already aligned on the right side.解决此问题的一种简单方法是使用 Angular Material 的mat-selection-listAngular Material list API 参考文档),然后排除其右侧的波纹效果,其中复选框已对齐。

component.ts组件.ts

testItems: string[] = ['Test 1', 'Test 2', 'Test 3', 'Test 4', 'Test 5'];

component.html组件.html

<mat-selection-list [disableRipple]="true" #item>
  <mat-list-option *ngFor="let test of testItems">
    {{test}}
  </mat-list-option>
</mat-selection-list>

The benefit of this is that you don't have to deal with default mat-checkbox styling and mat-selection-list is an Angular component too, resulting in an example on StackBlitz .这样做的好处是您不必处理默认mat-checkbox样式,并且mat-selection-list也是一个 Angular 组件,因此在StackBlitz 上有一个示例

Outcome of StackBlitz: StackBlitz 的结果:

StackBlitz 的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM