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:
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.
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.
<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.
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">
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With