0

I am in the need to set a value (variable) and get change after performing some specific task.
I would like to share my system environment, which is as below-

******************************************************
Your system information:

Cordova CLI: 6.4.0 
Ionic Framework Version: 2.0.0-beta.10
Ionic CLI Version: 2.1.8
Ionic App Lib Version: 2.1.4
ios-deploy version: Not installed
ios-sim version: 5.0.8 
OS: OS X Yosemite
Node Version: v6.2.2
Xcode version: Xcode 7.2 Build version 7C68
******************************************************

I wanted to use setter and getter to get this task done, see what i did-
mypage.ts-

import {NavController, NavParams, Content} from 'ionic-angular';
export class ListPage {
  constructor(private navCtrl: NavController, navParams: NavParams) {
    this.counter = 0;
  }
  public counter;

  getCounter(){
    return this.counter;
  }

  setCounter(count) {
    this.counter = count;
    // console.log(this.counter);
  }
}

and this is html template as-

<div *ngIf= "( getCounter()== '0')">
   .....
     ...  
      ..
     ...
   .....
</div>
<div (click)= (setCounter(getCounter()+1))
</div>
 some= line of codes
    ....
    ....
    ....
on bottom
<div ( setCounter(0))>
</div>   

I am able to get the value which is 0, but don't know how to set it. I want to set value without (click) even i have used click but i don't know how to do without click.

here are list of source URL which I have seen-

  1. How i can get input value within ionic 2 in my Component?
  2. Angular 2 call a function/method when an event is emitted
  3. How to call another components function in angular2
    List 2 was a little bit useful but can't help me out.

Hope for help

Community
  • 1
  • 1
S.Yadav
  • 4,273
  • 3
  • 37
  • 44

2 Answers2

1

See the link get and set in TypeScript

get Counter(){
    return this.counter;
  }

In HTML

<div *ngIf="Counter== '0'">
<div (click)= "setCounter(Counter+1)>"
Community
  • 1
  • 1
Habeeb
  • 1,020
  • 16
  • 35
  • Thanks for your effort @habeeb, very first- you have used wrong closing of quotes, so it's not going to work {.
    " ==> .
    }. 2. if you read carefully- I have mentioned that i don't want to to it on click.
    – S.Yadav Dec 30 '16 at 06:16
0

This example below will increase the counter each time the page is visited. ionViewDidEnter will be called each time page is viewed. Hope this is what you are lloking for.

export class ListPage {
  constructor(private navCtrl: NavController, navParams: NavParams) {
    this.counter = 0;
  }
  public counter;

  ionViewDidEnter(){
    this.setCounter(this.getCounter() + 1 ) ;
  }

  getCounter(){
    return this.counter;
  }

  setCounter(count) {
    this.counter = count;
    // console.log(this.counter);
  }
}
raj
  • 5,989
  • 7
  • 30
  • 62