【问题标题】:Get variable value from html in typescript ionic 2从打字稿离子2中的html获取变量值
【发布时间】:2017-08-17 01:30:15
【问题描述】:

我正在编写代码,但无法从 html 页面获取范围的值。我正在处理这个(我想设置设备的亮度):

  <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>

在我的 ts 中:

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

但该值始终是未定义的。那么如何从 html -> ts 发送?

【问题讨论】:

    标签: angular typescript ionic2 ionic3


    【解决方案1】:

    这个值是未定义的,因为它没有被初始化并且你试图从构造函数中获取那个值。尝试使用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>
    

    在你的代码中:

    public changeBrightness(): void {
      console.log(this.BrightnessValue);
      Brightness.setBrightness(this.BrightnessValue);
    }
    

    【讨论】:

    • 很高兴听到这个消息:)
    猜你喜欢
    • 2017-04-13
    • 2022-01-07
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    相关资源
    最近更新 更多