0

我正在使用 angular material 2 ( material.io ),并且我试图将 md-radio-button 的标签放在收音机本身的上方,如下所示:

单选按钮

文档说您可以轻松地将位置设置为之后或之前。我也在使用 flex-layout (github.com/angular/flex-layout)。

这是我的代码:

<md-radio-group formControlName="id_customer_type" (change)="onChangeCustomerType($event.value)">
  <md-radio-button type="radio" name="id_customer_type" value="1" [checked]="true"> Persona</md-radio-button>
  <md-radio-button type="radio" name="id_customer_type" value="2"> Empresa</md-radio-button>
  <md-radio-button type="radio" name="id_customer_type" value="3"> IoT/M2M</md-radio-button>
</md-radio-group>

如果有人可以帮助我,我将非常感激。谢谢!

4

2 回答 2

3

如果您仔细观察由您创建的元素,md-radio-group您会注意到:

<label class="mat-radio-label" for="md-radio-2-input">
    <div class="mat-radio-container">
        <div class="mat-radio-outer-circle"></div>
        <div class="mat-radio-inner-circle"></div>
        <div class="mat-radio-ripple mat-ripple" md-ripple="" ng-reflect-trigger="[object HTMLLabelElement]"
             ng-reflect-centered="true" ng-reflect-disabled="false"></div>
    </div>
    <input class="mat-radio-input cdk-visually-hidden" type="radio" id="md-radio-2-input" name="md-radio-group-0">
    <div class="mat-radio-label-content"><span style="display:none">&nbsp;</span>Option 2</div>
</label>

.mat-radio-label有这个CSS:

.mat-radio-label {
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    white-space: nowrap;
    vertical-align: middle;
}

这基本上告诉我们正在使用Flexbox来对齐其直接子项(单选按钮和标签)。默认flex-directionrow,因此省略。您可以将其更改为列。通过添加flex-direction: column-reverse,您将把标签放在上面。

于 2017-08-22T19:50:48.260 回答
2

这对我有用

:host /deep/ .mat-radio-label {
  flex-direction: column-reverse;
  .mat-radio-label-content {
    padding: 0;
  }
}

使用"@angular/material": "^6.4.7"

在此处输入图像描述

于 2018-11-15T18:15:40.033 回答