如何获取Angular Material滑块的当前值?

时间:2017-10-26 16:14:57

标签: angular angular-material

  

我的问题不同   Get mdslider value in angular 2?   因为我需要将滑块的值传递给组件,而不是   只需使用局部变量;而且还有角度材料   已更改,以便滑块现在需要$ event对象   传递给组件函数。

我在Angular 4中有一个Angular Material滑块组件:

HTML

<mat-slider min="430" max="500" step="10" value="450" (input)="pitch($event)"></mat-slider>

TS

import { Component, OnInit } from '@angular/core';
import { Inject, Injectable } from '@angular/core';
import { MatSlider } from '@angular/material';
import { AudioContext } from 'angular-audio-context';

@Component({
  selector: 'pitch-slider',
  templateUrl: './pitch-slider.component.html',
  styleUrls: ['./pitch-slider.component.css']
})


export class PitchSliderComponent implements OnInit {



  constructor(private _audioContext: AudioContext) { }

  ngOnInit() {
  }

}

我对Angular很新,我无法获得滑块的当前值。它似乎不适用于$ event对象?

2 个答案:

答案 0 :(得分:5)

我对$ event对象感到困惑 - 我需要使用component.ts中的event来引用它:

<强> HTML

<h2>freq.</h2>
  <button class="btn btn-primary" (click)="play()">Play</button>
  <mat-slider min="430" max="500" step="10" #matslider (input)="pitch($event)"></mat-slider>

      <div>
        {{ matslider.value }}
      </div>

<强> TS

import { Component, OnInit } from '@angular/core';
import { Inject, Injectable } from '@angular/core';
import { MatSlider } from '@angular/material';
import { AudioContext } from 'angular-audio-context';


@Component({
  selector: 'pitch-slider',
  templateUrl: './pitch-slider.component.html',
  styleUrls: ['./pitch-slider.component.css']
})


export class PitchSliderComponent implements OnInit {
  value: 450;

  constructor(private _audioContext: AudioContext) { }

  ngOnInit() {
  }

  pitch(event: any) {
  console.log(event.value);
}

  play() {
    console.log("play clicked!");
    var frequency = this.value;
    var volume = 0.2;
    var oscillator = this._audioContext.createOscillator();
    var gainNode = this._audioContext.createGain();

    oscillator.connect(gainNode);
    gainNode.connect(this._audioContext.destination);

    gainNode.gain.value = volume;
    oscillator.frequency.value = frequency;

    oscillator.start();

    setTimeout(
      function(){
        oscillator.stop();
      }, 500
    );
  }

}

答案 1 :(得分:1)

具有(输入)事件,当您在mat-slider之外单击时,仍会触发此事件。

我将其更改为(更改)事件。

这是我展示防滑垫价值的方式,我为谁分享关注的地方。

TS文件

export class SliderOverviewExample {
  gridsize: number = 30;
  updateSetting(event) {
    this.gridsize = event.value;
  }
}

HTML文件

<div>
    <span>Grid Size (px): </span><b class="gridSizeValue">{{gridsize.value}}</b>
</div>
<mat-slider #gridsize (change)="updateSetting($event)" 
      step="5" min="10" max="100" [value]="gridsize"></mat-slider>

演示https://stackblitz.com/edit/angular-display-mat-slider-value?file=app/slider-overview-example.html