<ion-checkboxlabelPlacement="start">Label at the Start</ion-checkbox><br/><ion-checkboxlabelPlacement="end">Label at the End</ion-checkbox><br/><ion-checkboxlabelPlacement="fixed">Fixed Width Label</ion-checkbox><br/><ion-checkboxlabelPlacement="stacked">Stacked Label</ion-checkbox>
Alignment
Developers can use the alignment property to control how the label and control are aligned on the cross axis. This property mirrors the flexbox align-items property.
注記
Stacked checkboxes can be aligned using the alignment property. This can be useful when the label and control need to be centered horizontally.
<ion-list><ion-item><ion-checkboxlabel-placement="stacked"alignment="start">Aligned to the Start</ion-checkbox></ion-item><ion-item><ion-checkboxlabel-placement="stacked"alignment="center">Aligned to the Center</ion-checkbox></ion-item></ion-list>
<ion-list><ion-item><ion-checkboxjustify="start">Packed at the Start of Line</ion-checkbox></ion-item><ion-item><ion-checkboxjustify="end">Packed at the End of Line</ion-checkbox></ion-item><ion-item><ion-checkboxjustify="space-between">Space Between Label and Control</ion-checkbox></ion-item></ion-list>
Checkbox labels can sometimes be accompanied with links. These links can provide more information related to the checkbox. However, clicking the link should not check the checkbox. To achieve this, we can use stopPropagation to prevent the click event from bubbling. When using this approach, the rest of the label still remains clickable.
<ion-checkbox>I agree to the <ahref="#"(click)="$event.stopPropagation()">terms and conditions</a></ion-checkbox>
Helper & Error Text
Helper and error text can be used inside of a checkbox with the helperText and errorText property. The error text will not be displayed unless the ion-invalid and ion-touched classes are added to the ion-checkbox. This ensures errors are not shown before the user has a chance to enter data.
In Angular, this is done automatically through form validation. In JavaScript, React and Vue, the class needs to be manually added based on your own validation.
<form[formGroup]="myForm"(ngSubmit)="onSubmit()"><ion-checkboxformControlName="agree"helperText="Agree to the terms before continuing"errorText="You must agree to the terms to continue"> I agree to the terms and conditions</ion-checkbox><br/><ion-buttontype="submit"size="small">Submit</ion-button></form>