Variable range in onfocus event of an Input

0

I'm working with ionic 2 and I have a variable that I want to access in an event in my html code, but by console I get it undefined, how do I get to reach that variable?

Code ts:

import { IonicPage, NavController, NavParams} from 'ionic-angular';

@IonicPage()
@Component({
  selector: 'page-productos',
  templateUrl: 'productos.html',
})
export class Productos {
  nItemCart = 14;
  constructor(public navCtrl: NavController, public navParams: NavParams) {

  }
ionViewDidLoad() {
    //console.log('ionViewDidLoad Productos');
  }
}

HTML code:

<input type="number" name="miinput" min="0" max="50" onfocus="console.log(nItemCart)" [(ngModel)]="producto.cantidad">

Result by Console:

undefined
    
asked by Daniel Enrique Rodriguez Caste 07.07.2017 в 23:23
source

1 answer

0

If I understand your question well, you want to access nItemCart at the time you focus (focus) the input. For that you can use the event (focus) from Angular 2:

View Demo

@Component({
  selector: 'my-app',
  template: '
    <input type="number" 
           min="0" 
           max="50" 
           (focus)="itemCart()">  
  ',
})
export class App {

  nItemCart:number = 0;

  constructor() {

    this.nItemCart = 1234;
  }

  itemCart() {
    console.log(this.nItemCart);
  }
}
    
answered by 08.07.2017 в 12:27