Abhishek Singh
Abhishek Singh

Reputation: 910

Get mdslider value in angular 2?

I have a input field in which i want to show the value of the matSlider

 <div class="col-lg-4 col-md-4 col-sm-12">
    <mat-slider class="example-full-width" 
    [max]=""
    [min]="min"
    [step]="step"
    [thumb-label]="thumbLabel"
    (input)="onInputChange($event)"></mat-slider>
  </div>
  <div class="col-lg-2 col-md-2 col-sm-12">
    <mat-form-field class="example-full-width">
      <input type="number" matInput formControlName="txtMinPriceRange"  placeholder="Minimum" [value]="minValue">
    </mat-form-field>  
  </div> 

right now I am using (input)="onInputChange($event)" is there any other way to do this

Upvotes: 3

Views: 934

Answers (1)

Vivek Doshi
Vivek Doshi

Reputation: 58533

Yes you can do it by 2 ways:

1) There is no need to call on component side, by using localVariable:

<div class="col-lg-4 col-md-4 col-sm-12">
    <mat-slider class="example-full-width" 
    [max]=""
    [min]="min"
    [step]="step"
    [thumb-label]="thumbLabel"
    #matslider></mat-slider>
</div>

<div class="col-lg-2 col-md-2 col-sm-12">
  <input type="number" placeholder="Minimum" [value]="matslider?.value">
</div> 

2) with Component side :

// component
export class SliderOverviewExample {
  matslider2 = 0;

  changeMatslider2(slider) {
    this.matslider2 = slider.value ;
  }
}

// template side
<div class="col-lg-4 col-md-4 col-sm-12">
    <mat-slider class="example-full-width" 
    [max]=""
    [min]="min"
    [step]="step"
    [thumb-label]="thumbLabel"
    (input)='changeMatslider2($event)'></mat-slider>
</div>

<div class="col-lg-2 col-md-2 col-sm-12">
  <input type="number" placeholder="Minimum" [value]="matslider2">
</div>

Here is the link to working demo with both implementation :

https://plnkr.co/edit/JlTyXM?p=preview

Upvotes: 3

Related Questions