Hi Pals,Im stucked here Failing to map Form Model into Data Model Here is my Template driven Form
<ion-content padding>
<form #commentForm ="ngForm" (ngSubmit)="onSubmit(form)" novalidate>
<ion-item>
<ion-label floating> Your Name </ion-label>
<ion-input type="text" required [(ngModel)]="todo.author" name="author"></ion-input>
</ion-item>
<ion-item>
<ion-label stacked> Your Rating </ion-label>
<ion-range min="1" max="5" step="1" pin="true" snaps="true" [(ngModel)]="todo.rating" name="rating">
<ion-icon range-left small name="sad"></ion-icon>
<ion-icon range-right name="happy"></ion-icon>
</ion-range>
</ion-item>
<ion-item>
<ion-label floating> Your Comment </ion-label>
<ion-textarea rows=12 required [(ngModel)]="todo.comment" name="comment"></ion-textarea>
</ion-item>
<button ion-button type="submit" [disabled]="commentForm.invalid" (click)="onSubmit()">Submit</button>
</form>
</ion-content>
Hence i Want to push every data that i feed in my Template Driven Form to this Data Model (i.e) every data which are author,rating and comment to be pushed to the Data Model below
<ion-header>
...
<ion-content padding>
<ion-card *ngIf="dish">
<img src="{{BaseURL + dish.image}}"/>
...
<ion-row>
<ion-col>
<button ion-button icon-left clear small>
<ion-icon name="star"></ion-icon>
<div>{{ avgstars }} stars</div>
</button>
</ion-col>
<ion-col>
<button ion-button icon-left clear small>
<ion-icon name="text"></ion-icon>
<div>{{ numcomments }} Comments</div>
</button>
</ion-col>
</ion-row>
</ion-card>
<ion-list *ngIf="dish">
<ion-list-header> Comments </ion-list-header>
<ion-item *ngFor="let comment of dish.comments" text-wrap>
<h4> {{ comment.comment }}</h4>
<p> {{comment.rating}} Stars</p>
<p>
<span> -- {{comment.author}} {{comment.date | date}}</span>
</p>
</ion-item>
</ion-list>
</ion-content>
refer the following as the Typescript file for the Template Driven Form Model
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ViewController } from 'ionic-angular';
import { Dish } from '../../shared/dish';
import { Comment } from '../../shared/comment';
@IonicPage()
@Component({
selector: 'page-comment',
templateUrl: 'comment.html',
})
export class CommentPage {
todo = {
author:'',
rating: 5,
comment: ''
};
constructor(public navCtrl: NavController, public navParams: NavParams,
public viewCtrl: ViewController,
) {}
....
onSubmit() {
this.comment = this.commentForm.value;
this.comment.date = new Date().toISOString();
console.log(this.comment);
this.dish.comments.push(this.comment);
this.commentForm.reset({
author:'',
rating: 5,
comment: ''
});
}
}
Honestly, I'm new to Ionic 3 especially on Maping Form Model to Data Model,hence any one can share with me some skills so that i can solve this,I'm open.