1

For one of my projects I have implemented an input range slider with custom styling. when the component initializes I want to set max price as the default value in the slider. I tried a couple of ways but I couldn't find a solution for that.can someone tells what I am doing wrong.this is stackblitz demo of my implementation

9
  • stackblitz.com/edit/create-a-basic-angular-component-3bcp5j Commented Apr 10, 2020 at 14:30
  • It looked a bit overcomplicated: Just bind a single formControl on the slider element and use the setValue method. You were correctly passing along the data as Inputs Commented Apr 10, 2020 at 14:32
  • @RobinDeSchepper nothing has changed in above link.i think you forgot to save Commented Apr 10, 2020 at 14:33
  • Argh, my wifi, lost all changes :( gimme a sec Commented Apr 10, 2020 at 14:34
  • I have to get that value changes as a stream that why used from control then I can subscribe to value changes.btw before I post here I tried setValue() and patchValue() both either of those not worked for me. Commented Apr 10, 2020 at 14:35

2 Answers 2

2

you need to create an input property for the value and set the formcontrol and bind this to input elemnt like this component

  @Input() value:number; // default value

  constructor(){
    this.inputForm = new FormGroup({priceRangeSlider : new FormControl(null)});
  }

  ngOnInit() {
    this.slider.valueChanges.subscribe(values =>{
      this.priceForDisplay = values;
    })

    this.inputForm.get('priceRangeSlider').setValue(this.value); // init value set 
  }

template

    <div class="slider-container">
      <input
        class="slider"
        type="range"
        formControlName="priceRangeSlider"
        [min]="minPrice"
        [max]="maxPrice"
        step="100"
        [value]="value"
      />
    </div>

app template

<app-user [minPrice]="1000" [maxPrice]="10000"  [value]="4000"><app-user>

demo 🚀

Sign up to request clarification or add additional context in comments.

Comments

1

I edited your code example: https://stackblitz.com/edit/create-a-basic-angular-component-amnhih

I bound a FormControl to the slider element and moved the initial setValue call to below the subscription.

Comments

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.