我在我的html页面中有这样的内容:
<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>正如您在这里看到的,默认情况下有一个段落的输入,用户可以选择添加任意数量的另一个段落。
这是我的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);
}使用这些代码,我只能在如下的一维中显示数据:
{
"title":"testing title",
"paragraph":"testing paragraph 1"
}但我希望它能以这种形式展示:
{
"title":"testing title",
"paragraph":[
{"paragraph": "testing paragraph 1"},
{"paragraph": "testing paragraph 2"},
{"paragraph": "testing paragraph 3"}
]
}发布于 2018-05-16 09:10:27
我建议如下:
类:
data = {
"title": "testing title",
"paragraphs": [
{ "paragraph": "testing paragraph 1" },
{ "paragraph": "testing paragraph 2" },
{ "paragraph": "testing paragraph 3" }
]
}
...
addParagraph(){
this.data.paragraphs.push( { "paragraph": "" });
}
delParagraph(paragraphID){
this.data.paragraphs.splice(paragraphID,1);
}HTML
<ion-row padding *ngFor="let paragraph of data.paragraphs; let i=index">
<ion-col col-11 class="pharagraph margin-bottom">
<ion-textarea rows="10" class="bordered" placeholder="Enter Content Paragraph" [(ngModel)]="paragraph.paragraph"></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>发布于 2018-05-16 09:18:41
你好,我想你想要定义一个段落数组而不是字符串。
this.data.title = '';
this.data.paragraph = [{}] instead of '';在this.data.paragraph中推送你的段落;)
this.data.paragraph.push({paragraph: 'My text content'});https://stackoverflow.com/questions/50366172
复制相似问题