Get mdslider value in angular 2?

孤街浪徒 提交于 2019-12-10 19:39:32

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/46906219/get-mdslider-value-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!