【问题标题】:Input Type="Range" in Reactive Forms in Angular在 Angular 的反应形式中输入类型 =“范围”
【发布时间】:2019-10-11 11:49:16
【问题描述】:

我在 Angular 中以反应形式设置滑块/范围的背景时遇到问题。输入值正在改变但问题是当我在输入上键入时,范围正在改变但颜色(绿色)没有改变。这就是问题所在,请参阅下面的这张图片并在此处查看此 stackblitz 链接:如果不必要,请修改我的代码。

CLICK HERE

【问题讨论】:

    标签: angular angular-reactive-forms angular-forms


    【解决方案1】:

    尝试将您的代码更新为:working stackblitz
    https://stackblitz.com/edit/slider-range-reactive-forms-jmoxcm

     changeInputTip(value: any) {
        console.log(+value);
        this.form.get("tip").setValue(+value);
        // this.slider2.nativeElement = +value;
        this.setValues();
      }
    
    setValues() {
          const slider = this.slider2.nativeElement as HTMLElement;
          const sliderValue = slider as HTMLInputElement;
          slider.style.background =
            "linear-gradient(to right, #f36621 0%, #f36621 " +
            sliderValue.value +
            "%, #eeeeee " +
            sliderValue.value +
            "%, #eeeeee)";
          // console.log(sliderValue.value);
    
          parseInt(sliderValue.value, 10) >= 20
            ? (this.circle1.nativeElement.style.background = "#f36621")
            : (this.circle1.nativeElement.style.background = "#cbcbcb");
    
          parseInt(sliderValue.value, 10) >= 40
            ? (this.circle2.nativeElement.style.background = "#f36621")
            : (this.circle2.nativeElement.style.background = "#cbcbcb");
    
          parseInt(sliderValue.value, 10) >= 60
            ? (this.circle3.nativeElement.style.background = "#f36621")
            : (this.circle3.nativeElement.style.background = "#cbcbcb");
    
          parseInt(sliderValue.value, 10) >= 80
            ? (this.circle4.nativeElement.style.background = "#f36621")
            : (this.circle4.nativeElement.style.background = "#cbcbcb");
    
          parseInt(sliderValue.value, 10) >= 100
            ? (this.circle5.nativeElement.style.background = "#f36621")
            : (this.circle5.nativeElement.style.background = "#cbcbcb");
    }
    
      ngAfterViewInit() {
        const slider = this.slider2.nativeElement as HTMLElement;
        console.log(slider);
        slider.oninput = () => {
          this.setValues();
        };
      }
    

    【讨论】:

    • 谢谢。有没有更好的方法来做到这一点?
    • @Joseph 如果解决了您的问题,您介意将其设置为正确答案吗?
    猜你喜欢
    • 2018-08-24
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    相关资源
    最近更新 更多