I am pretty new to angular/ionic and I am trying to create a form with checkboxes that are checked by default.
It tried both
checked
and
checked="true"
but neither is working. The strange thing is, that the attribute 'disabled' works just fine.
Here is my html:
<ion-header>
<ion-toolbar>
<ion-title>sold-items</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<form (ngSubmit)="addSoldItem()" name="addItemForm">
<ion-item>
<ion-label>Name</ion-label>
<ion-input type="text" [(ngModel)]="soldItem.item" name="item"></ion-input>
</ion-item>
<ion-item>
<ion-label>Brutto</ion-label>
<ion-input type="number" [(ngModel)]="soldItem.gross" name="gross"></ion-input>
</ion-item>
<ion-item>
<ion-label>eBay</ion-label>
<ion-checkbox checked [(ngModel)]="soldItem.ebay" name="ebay"></ion-checkbox>
</ion-item>
<ion-item>
<ion-label>PayPal</ion-label>
<ion-checkbox checked="true" [(ngModel)]="soldItem.paypal" name="paypal"></ion-checkbox>
</ion-item>
<ion-item>
<ion-label>Provision</ion-label>
<ion-checkbox checked="true" [(ngModel)]="soldItem.commission" name="commission"></ion-checkbox>
</ion-item>
<ion-item>
<ion-label>Versand soll</ion-label>
<ion-input type="number" [(ngModel)]="soldItem.shipping_must" name="shipping_must"></ion-input>
</ion-item>
<ion-item>
<ion-label>Versand ist</ion-label>
<ion-input type="number" [(ngModel)]="soldItem.shipping_is" name="shipping_is"></ion-input>
</ion-item>
<button ion-button type="submit" block>Hinzufügen</button>
</form>
<ion-card *ngFor="let soldItem of (soldItems | async)?.slice().reverse()">
<ion-card-header>
<ion-card-title>{{ soldItem.item }}</ion-card-title>
<ion-card-subtitle>€ {{ soldItem.gross }}</ion-card-subtitle>
</ion-card-header>
<ion-card-content>
<p>
<b>Netto: €</b> {{ soldItem.net }}
</p>
<p>
<b>eBay: €</b> {{ soldItem.ebay_fees }}
</p>
<p>
<b>PayPal: €</b> {{ soldItem.paypal_fees }}
</p>
<p>
<b>Versand soll: €</b> {{ soldItem.shipping_must }}
</p>
<p>
<b>Versand ist: €</b> {{ soldItem.shipping_is }}
</p>
<p>
<b>Verkauft am: </b> {{ soldItem.date_sold }}
</p>
</ion-card-content>
</ion-card>
</ion-content>
This is what it looks like:
I am using angular8 and ionic4 on a MacBook Pro with the latest Safari Browser. Any ideas?
It is similar to HTML checkbox inputs. The Ionic checkboxes are styled differently on each platform as like other Ionic components. You can use checked attribute with the <ion-checkbox> element to set the default value, and disabled attribute to disable the user from changing the value.
There's a ngModel
on the checkbox, so that will determin the checked
value.
<ion-checkbox [(ngModel)]="soldItem.ebay" name="ebay"></ion-checkbox>
The value of soldItem.ebay
will determine the checked
state. If that is set to true, the checkbox
will be checked
to start with.
On an unrelated note. Consider using reactive forms.
Reactive forms provide more predictability with synchronous access to the data model, immutability with observable operators, and change tracking through observable streams. If you prefer direct access to modify data in your template, template-driven forms are less explicit because they rely on directives embedded in the template, along with mutable data to track changes asynchronously
A guide to the key diffences betweem template driven (what you have now) and reactive forms.
In general:
- Reactive forms are more robust: they're more scalable, reusable, and testable. If forms are a key part of your application, or you're already using reactive patterns for building your application, use reactive forms.
- Template-driven forms are useful for adding a simple form to an app, such as an email list signup form. They're easy to add to an app, but they don't scale as well as reactive forms. If you have very basic form requirements and logic that can be managed solely in the template, use template-driven forms.
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