Trip Therapy Trip Therapy -4 years ago 244
AngularJS Question

Get variable value from html in typescript ionic 2

I'm writing a code but I can't get a value of a range from html page. I'm working on this(I want to set the brightness of a device) :

<ion-item>
<ion-range [(ngModel)]="BrightnessValue" color="dark" pin="true" step="1" min="0" max="10">
<ion-icon range-left small name="sunny"></ion-icon>
<ion-icon range-right name="sunny"></ion-icon>
</ion-range>
</ion-item>


In my ts :

......
BrightnessValue: number ;
....
constructor(....){
console.log(this.BrightnessValue);
Brightness.setBrightness(this.BrightnessValue);
}


But the value is always undefinde. So how can I send from html -> ts ?

Answer Source

The value is undefined because it was not initialized and you're trying to get that value from the constructor. Try by using the ionChange event:

 <ion-item>
    <ion-range (ionChange)="changeBrightness()" [(ngModel)]="BrightnessValue" color="dark" pin="true" step="1" min="0" max="10">
      <ion-icon range-left small name="sunny"></ion-icon>
      <ion-icon range-right name="sunny"></ion-icon>
    </ion-range>
  </ion-item>

And in your code:

public changeBrightness(): void {
  console.log(this.BrightnessValue);
  Brightness.setBrightness(this.BrightnessValue);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download