Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 /Ionic2: databinding

Tags:

angular

ionic2

I have a simple component as follow:

import {Page} from 'ionic-framework/ionic';
import {Component} from 'angular2/core';
import {Data} from './data';

@Page({
            selector: "main-page",
            templateUrl: 'build/pages/main/main.html'
})

export class MainPage 
{
    submitted: number;
    model: Array<number>;

    constructor(){
       this.submitted = false;
    }
     model = new Data('50', ['10', '20']);
      onSubmit()
    {
        this.model.households.push('3');
    }

    backToForm()
    {
        this.submitted = false;
    }
}

with the following template: MainPage

<ion-content padding class="getting-started" >

      <h1>Random selection of houses generator</h1>
      <div [hidden]='submitted'>
        <form (ngSubmit)="onSubmit()" #dataForm="ngForm">
            <ion-list>
              <ion-item>
                <ion-label floating>Number of houses in the village</ion-label>
                <ion-input 
                      type="number"
                      required
                      [(ngModel)]="model.totalhousehold"
                      ngControl = "totalhousehold"
                    ></ion-input>
              </ion-item>
            </ion-list>
            <button 
                    secondary
                    [disabled]="!dataForm.form.valid"
                    >Generate random selection!</button>
        </form>
      </div>

      <div [hidden]='!submitted'>
          villages: {{ model.totalhousehold }}

          Array: {{ model.households }}


          <button (click)=backToForm()>
              back to form
          </button>
      </div>

</ion-content>

this is my data.ts:

export class Data {

    constructor(
        public  totalhousehold:      number,
        public households: Array<number>,
     ) { }
}

On each submit I push a value in my "households" array and hide the form. Back to Form display the form. If I modify the form value (totalhouseholds), my Array (households) is displayed correctly. But it I submit again without modifying the value; the displayed array is not updated. Any idea advice be welcome

like image 922
Raphael_b Avatar asked Dec 28 '25 16:12

Raphael_b


1 Answers

I suspect that model should be declared as Object instead of having it Array<number> as it do have two properties.

Other thing is when you try to do this.model.households.push('3'); technically it should been throw an error as in model.households is of type Array<number>(which will only allow to accept number). Because latest version of Javascript it won't allow to put string in place of number(If its typescript then you must have get those error at compile time). I'm wondering how that got worked in first place.

For showing object value on HTML, you could use json pipe to show it.

{{model.households | json}}
like image 95
Pankaj Parkar Avatar answered Dec 30 '25 22:12

Pankaj Parkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!