代码之家  ›  专栏  ›  技术社区  ›  GoldenAge

在Angular2中移动HTML滑块时动态更新变量值

  •  5
  • GoldenAge  · 技术社区  · 6 年前

    我在Angular6应用程序中使用HTML圆形滑块: https://www.w3schools.com/howto/howto_js_rangeslider.asp

    .html文件中的实现如下:

    <span style="font-size: 130px;">{{sliderVal}}</span>
    
    <input type="range" #slider min="1" max="10" value="1" class="slider" (change)="onPriceSliderChange(slider.value)">
    

    .ts文件的实现:

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-sample,
      templateUrl: './sample.component.html',
      styleUrls: ['./sample.component.scss']
    })
    export class SampleComponent {
      public sliderVal = 1;
      constructor() { }
    
      onPriceSliderChange(val) {
        this.sliderVal = val;
      }
    
    }
    

    只有当移动滑块并取消按鼠标左键时,SliderVal值才会更改。每次移动滑块时,我都要动态更新SliderVal的值,而不仅仅是在取消按下鼠标按钮时。我知道如何使用jquery实现这一点,但我不想将jquery解决方案与Angular混合使用。有什么想法吗?干杯

    1 回复  |  直到 6 年前
        1
  •  3
  •   Vikhyath Maiya    6 年前

    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
    public sliderVal = 1;
      constructor() {
       }
    
      onPriceSliderChange(val) {
        this.sliderVal = val;
      }
    
    }
    

    <span style="font-size: 130px;">{{sliderVal}}</span>
    
    <input type="range" #slider min="1" max="10" value="1" [(ngModel)] ="sliderVal" class="slider" (change)="onPriceSliderChange(slider.value)">
    

    Here