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
2 Answers
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>
Comments
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.
setValuemethod. You were correctly passing along the data asInputssubscribeto value changes.btw before I post here I triedsetValue()andpatchValue()both either of those not worked for me.