Académique Documents
Professionnel Documents
Culture Documents
Template-driven Approach
Angular 2 automatically created the JavaScript representation of your form
for you whenever it detects a form element from your HTML code.
Reactive Approach
You create the form on your own programmatically, then you tell ionic
2 which form and form element in your HTML template it has to sync
with your form object and controls.
Template-driven Approach
2. Registering controls
• Add ngModel to the input elements, and do not forget to specify the
name
3. Submitting the form
• Edit the form element to have a local reference, and assign ngForm to
the reference to tell Angular that we want access
• Bind (ngSubmit)=“” in the form to add function to be fired when the
form is submitted
4. Accessing the data
• Receive the value of the submitted data using form.value.InputName
Preparing the HTML Form
<form>
<ion-list>
<ion-item>
<ion-label fixed>Username:</ion-label>
<ion-input type="text" name="username" required
placeholder="Username"></ion-input>
</ion-item>
<ion-item>
<ion-label fixed>Password</ion-label>
<ion-input type="password" name="password" required
placeholder="Password"></ion-input>
</ion-item>
<ion-item>
<ion-label fixed>Subscribed</ion-label>
<ion-toggle name="toggle"></ion-toggle>
</ion-item>
</ion-list>
<button ion-button type="submit" block>Save</button>
</form>
Registering Controls
<form>
<ion-list>
<ion-item>
<ion-label fixed>Username:</ion-label>
<ion-input type="text" name="username" required
placeholder="Username" ngModel></ion-input>
</ion-item>
<ion-item>
<ion-label fixed>Password</ion-label>
<ion-input type="password" name="password" required
placeholder="Password" ngModel></ion-input>
</ion-item>
<ion-item>
<ion-label fixed>Subscribed</ion-label>
<ion-toggle name="toggle" ngModel></ion-toggle>
</ion-item>
</ion-list>
<button ion-button type="submit" block>Save</button>
</form>
Submitting the Form
<form #f="ngForm" (ngSubmit)="onAddUser(f)">
<ion-list>
<ion-item>
<ion-label fixed>Username:</ion-label>
<ion-input type="text" name="username" required placeholder="Username"
ngModel></ion-input>
</ion-item>
<ion-item>
<ion-label fixed>Password</ion-label>
<ion-input type="password" name="password" required
placeholder="Password" ngModel></ion-input>
</ion-item>
<ion-item>
<ion-label fixed>Subscribed</ion-label>
<ion-toggle name="toggle" ngModel></ion-toggle>
</ion-item>
</ion-list>
<button ion-button type="submit" block [disabled]="!f.valid">Save</button>
</form>
TS
onAddUser(form: NgForm) {
console.log(form.value)
}
Reactive Approach
https://angular.io/api/forms/FormGroup
https://angular.io/api/forms/FormControl
https://angular.io/api/forms/Validators
Preparing the HTML Form
<form>
<ion-list>
<ion-item>
<ion-label fixed>Username</ion-label>
<ion-input type="text"></ion-input>
</ion-item>
<ion-item>
<ion-label fixed>Password</ion-label>
<ion-input type="password"></ion-input>
</ion-item>
<ion-item>
<ion-label fixed>Class of</ion-label>
<ion-select>
<ion-option value="2017">2017</ion-option>
<ion-option value="2016">2016</ion-option>
<ion-option value="2015">2015</ion-option>
<ion-option value="2014" selected>2014</ion-option>
</ion-select>
</ion-item>
</ion-list>
<button ion-button type="submit" block color="secondary">Register me</button>
</form>
Preparing the Reactive Form
userForm: FormGroup;
ngOnInit() {
this.initializeForm()
}
private initializeForm() {
this.userForm = new FormGroup({
username: new FormControl(null, Validators.required),
password : new FormControl(null, Validators.required),
classOf: new FormControl("2014", Validators.required)
})
}
onSubmit() {
console.log(this.userForm.value)
console.log(this.userForm.value.paymentStatus[0])
}
Synchronizing the Reactive Form and the HTML Form
<form [formGroup]="userForm" (ngSubmit)="onSubmit()">
<ion-list>
<ion-item>
<ion-label floating>Username</ion-label>
<ion-input type="text" formControlName="username"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Password</ion-label>
<ion-input type="password" formControlName="password"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Class of</ion-label>
<ion-select formControlName="classOf">
<ion-option value="2017">2017</ion-option>
<ion-option value="2016">2016</ion-option>
<ion-option value="2015">2015</ion-option>
<ion-option value="2014" selected>2014</ion-option>
</ion-select>
</ion-item>
</ion-list>
<button ion-button type="submit" block [disabled]=“!userForm.valid">Register me</button>
</form>
TS
export class RegisterPage implements OnInit{
userForm: FormGroup;
ngOnInit() {
this.initializeForm()
}
private initializeForm() {
this.userForm = new FormGroup({
username: new FormControl(null, Validators.required),
password : new FormControl(null, Validators.required),
classOf: new FormControl("2014", Validators.required)
})
}
onSubmit() {
console.log(this.userForm.value)
}
}
Angular FormArray
https://angular.io/api/forms/FormArray
<ion-list formArrayName="paymentStatus">
<ion-list-header color="primary">Payment Status</ion-list-header>
<ion-item>
<ion-label fixed>SKS</ion-label>
<ion-checkbox value="sks" formControlName="0"></ion-checkbox>
</ion-item>
<ion-item>
<ion-label fixed>Bangunan</ion-label>
<ion-checkbox value="bangunan" formControlName="1"></ion-checkbox>
</ion-item>
</ion-list>
https://ionicframework.com/docs/api/components/toast/ToastController/
Toast
A Toast is a subtle notification commonly
used in modern applications.
Can be used to provide feedback about an
operation or to display a system message.
Action Sheet
in Ionic
https://ionicframework.com/docs/api/components/action-sheet/ActionSheetController/
Action Sheet
Action Sheet
An Action Sheet is a dialog that lets the user choose from a set of
options.
Reactive Approach
Angular FormGroup, FormControl, and FormArray