1

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 ?

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Glad to hear that :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.