Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lablel for md-radio-group

I have a group of radio buttons in an Angular 4 Material app:

<md-radio-group fxLayout fxLayoutAlign="center center" fxLayoutGap="30px">
    <md-radio-button value="1">Date</md-radio-button>
    <md-radio-button value="2">Status</md-radio-button>
    <md-radio-button value="3">From</md-radio-button>
    <md-radio-button value="4">To</md-radio-button>
</md-radio-group>

I went through the docs and samples and wasn't able to figure out how to give a label to this group. I need something like this but with proper styling: enter image description here

What is the intended way in Angular Material of adding a label to a radio group?

Obviously, we could add a label and style it from scratch, but it doesn't look terribly productive for the framework of heavily styled components.

like image 433
Alexander Abakumov Avatar asked Oct 05 '17 15:10

Alexander Abakumov


People also ask

What are radio buttons and checkboxes?

Checkboxes and radio buttons are elements for making selections. Checkboxes allow the user to choose items from a fixed number of alternatives, while radio buttons allow the user to choose exactly one item from a list of several predefined alternatives.

What is mat radio button?

<mat-radio-button> provides the same functionality as a native <input type="radio"> enhanced with Material Design styling and animations. All radio-buttons with the same name comprise a set from which only one may be selected at a time.


2 Answers

This is what you are looking for.

<div fxLayout fxLayoutAlign="center center" fxLayoutGap="30px">
      <label>Sort by</label>  
      <md-radio-group>
        <md-radio-button value="1">Date</md-radio-button>
        <md-radio-button value="2">Status</md-radio-button>
        <md-radio-button value="3">From</md-radio-button>
        <md-radio-button value="4">To</md-radio-button>
      </md-radio-group>
  </div>

css

.mat-radio-button {margin-right: 30px;}

else add flexLayout to your md-radio-group

<md-radio-group fxLayout fxLayoutAlign="center center" fxLayoutGap="30px">
like image 82
Mike Trinh Avatar answered Oct 27 '22 11:10

Mike Trinh


From https://codepen.io/jakobadam/pen/xqZoBR

<!--
  `md-input-has-value` elevates `label`
  `md-input-invalid` shows error message
-->
<md-input-container class="md-input-has-value" 
                    ng-class="{ 'md-input-invalid' : form.fruit.$invalid && form.$submitted }">
  <label class="md-required" translate>Fruit</label>

  <md-radio-group md-autofocus name="fruit" ng-model="fruit" layout="row" required>
    <md-radio-button value="Apple" class="md-primary">Apple</md-radio-button>
    <md-radio-button value="Banana"> Banana </md-radio-button>
    <md-radio-button value="Mango">Mango</md-radio-button>  
  </md-radio-group>

  <div ng-messages="form.fruit.$error">
    <div ng-message="required" translate>Yo. Select some fruit.</div>
  </div>
</md-input-container>
like image 26
Kunal Panchal Avatar answered Oct 27 '22 12:10

Kunal Panchal