I have this in my html side :
<ion-grid style="margin-bottom: 25px;">
<ion-row padding>
<ion-col col-11>
<label class="bold">Title</label>
<ion-input type="text" class="bordered" placeholder="Enter Title" [(ngModel)]="data.title"></ion-input>
</ion-col>
</ion-row>
<ion-row padding>
<ion-col col-11 class="pharagraph margin-bottom">
<label class="bold">Pharagraph</label>
<ion-textarea rows="10" class="bordered" placeholder="Enter Content Paragraph" [(ngModel)]="data.paragraph"></ion-textarea>
</ion-col>
</ion-row>
<ion-row padding *ngFor="let input of inputs; let i=index">
<ion-col col-11 class="pharagraph margin-bottom">
<ion-textarea rows="10" class="bordered" placeholder="Enter Content Paragraph" [(ngModel)]="data.paragraph[i]"></ion-textarea>
</ion-col>
<ion-col col-1>
<button ion-button clear icon-start icon-right (click)="delParagraph(i)"><ion-icon name="trash" color="danger"></ion-icon></button>
</ion-col>
</ion-row>
<ion-row padding>
<ion-col col-11>
<button ion-button block color="danger" id="addPharagraph" (click)="addParagraph()"><ion-icon name="add-circle"></ion-icon> add pharagraph</button>
</ion-col>
</ion-row>
</ion-grid>
<div class="tab" style="position: fixed; bottom: 0;">
<a class="tab-item" (click)="save()">Save Draft</a>
</div>
As you can see here, there is an input for a paragraph by default and user can choose to add another paragraph as many as they want.
Here is my TypeScript :
data:any = {};
constructor(public navCtrl: NavController, public navParams: NavParams, public http: Http) {
this.data.title = '';
this.data.paragraph = '';
}
addParagraph(){
this.inputs.splice(0,0,this.inputs.length+1);
}
delParagraph(paragraphID){
this.inputs.splice(this.inputs.indexOf(paragraphID), 1);
}
save(){
var testData = JSON.stringify(this.data);
console.log(testData);
}
With those code, I can only display the data in 1 dimension like this :
{
"title":"testing title",
"paragraph":"testing paragraph 1"
}
But I wanted it to be displayed in this form :
{
"title":"testing title",
"paragraph":[
{"paragraph": "testing paragraph 1"},
{"paragraph": "testing paragraph 2"},
{"paragraph": "testing paragraph 3"}
]
}