2017-03-16 1 views
0

ユーザ入力の動的リストがあります。ユーザは、入力フィールドを生成する「宛先の追加」をクリックすることにより、宛先を追加することができる。フィールドが生成されると、ユーザーは宛先を入力し、すべての宛先を配列に格納するボタンをクリックします。ユーザーは複数の宛先を追加できるはずです。ionic2/angular2を使用して複数の入力を配列に格納するには

<ion-header> 
    <ion-navbar> 
    <ion-title>Home</ion-title> 
    </ion-navbar> 
</ion-header> 

<ion-content padding> 
    <form> 
    <ion-list> 
    <ion-item> 
    <ion-label fixed>Starting</ion-label> 
    <ion-input type="text" ></ion-input> 
    </ion-item> 

    <ion-item *ngFor="let destination of destinations" > 
    <ion-label fixed>Destination</ion-label> 
    <ion-input type="text" ></ion-input> 
    </ion-item> 
    </ion-list> 


    <button ion-button color="light" icon-right (click)="addDestination()" > 
     Add Destination 
     <ion-icon name="add" ></ion-icon> 
    </button> 
    <div style="padding-bottom: 10px; position: absolute; bottom: 0px; width: 92%"> 
    <button ion-button block (click)="store()">Find Path</button> 
    </div> 
    </form> 
</ion-content> 

活字体:私は私のコメントで何を意味するか

import { Component } from '@angular/core'; 

import { NavController } from 'ionic-angular'; 

@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 
export class HomePage { 
    public destinations = []; 

    constructor(public navCtrl: NavController) { 

    } 





    addDestination(){ 
    this.destinations.push(1); 
    console.log(this.destinations); 
    console.log('hello'); 
    } 

    store(){ 
    console.log('hello'); 
    console.log(this.destinations); 
    } 

} 
+0

ngModelがなる=地[$インデックス]。したがって、各入力には配列の値が保持されています – yBrodsky

+0

はangular2の値ですか? – Dansmith12

答えて

1

<div *ngFor="let destination of destinations; let i = index"> 
    <input type="text" [(ngModel)]="destinations[i]"/> 
</div> 

https://plnkr.co/edit/mZi1UYI9drNFRwVQsJs5?p=preview

+0

これは部分的にメイトのおかげで働いています!しかし問題は、現在、宛先を追加するたびに、すべての入力フィールドがすべての入力からクリアされることです。入力をフィールドに保存する方法はありますか? – Dansmith12

+0

あなたは何を意味するのか分かりません。私のための作品:https://plnkr.co/edit/ZzU3StsH7p4QNKvtPGTQ?p=preview – yBrodsky

+0

あなたの解決策では、ボックスのいずれかに入力を追加すると、それは他のボックスを埋めます。 – Dansmith12

関連する問題