How pass data from one form to another form on ion-input in Ionic 3?

Viewed 1777

I want to pass data from one page to another page on ion-input. I'm using ionic 3. Here is my code...

home.ts file

viewEntry(ID, name, description){

    alert(ID);
    alert(name);
    alert(description);

    this.navCtrl.push(AddtechnologyPage, {ID: ID, name: name, description: description});
}

addtechnology.ts file

constructor(public navCtrl: NavController,
          public navParams: NavParams,
          public http: Http,
          public fb: FormBuilder,
          public toastCtrl: ToastController) {

     this.recordID = navParams.get('ID');
     this.technologyName = navParams.get('name');
     this.technologyDescription = navParams.get('description');


     console.log(this.recordID);
     console.log(this.technologyName);
     console.log(this.technologyDescription);


    // Create form builder validation rules
    this.form = fb.group({
        "name"                  : ["", Validators.required],
        "description"           : ["", Validators.required]
    });
}

addtechnology.html file

<form [formGroup]="form">

      <ion-list>
         <ion-item-group>
            <ion-item-divider color="light">Technology Name *</ion-item-divider>
            <ion-item>
               <ion-input
                  type="text"
                  placeholder="Enter a name..."
                  formControlName="name"
                  [(ngModel)]="technologyName" ></ion-input>
            </ion-item>
         </ion-item-group>
         <ion-item-group>
            <ion-item-divider color="light">Technology Description *</ion-item-divider>
            <ion-item>
               <ion-textarea
                  placeholder="Description..."
                  formControlName="description"
                  rows="6"
                  [(ngModel)]="technologyDescription"></ion-textarea>
            </ion-item>
         </ion-item-group>


         <ion-item>
            <button
               ion-button
               color="primary"
               text-center
               block
               [disabled]="!form.valid"
               (click)="saveEntry()">Save Entry</button>
         </ion-item>

      </ion-list>

   </form>

I want to display data on ion-input of addtechnology.html. Data is displaying in addtechnolgy.ts file but how to bind/display on ion-input? Thanks in advance.

1 Answers

You could pass the values when initializing the form, like this:

// Create form builder validation rules
this.form = fb.group({
    "name": [this.technologyName, Validators.required],
    "description": [this.technologyDescription, Validators.required]
});

And then in each input, just use the form control (instead of both form control and ngModel):

      <ion-item-group>
        <ion-item-divider color="light">Technology Name *</ion-item-divider>
        <ion-item>
           <ion-input
              type="text"
              placeholder="Enter a name..."
              formControlName="name"></ion-input>
        </ion-item>
     </ion-item-group>
     <ion-item-group>
        <ion-item-divider color="light">Technology Description *</ion-item-divider>
        <ion-item>
           <ion-textarea
              placeholder="Description..."
              formControlName="description"
              rows="6"></ion-textarea>
        </ion-item>
     </ion-item-group>
Related