简体   繁体   English

从打字稿离子2中的html获取变量值

[英]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. 我正在编写代码,但无法从html页面获取范围值。 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 : 在我的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 ? 那么如何从html-> ts发送呢?

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: 尝试使用ionChange事件:

 <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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM