如何获取“角度材质”滑块的当前值?

Dav*_*983 2 angular-material angular

我的问题与在角度2中获取mdslider值不同 我需要将滑块的值传递给组件,而不仅仅是使用局部变量。而且还更改了Angular Material,因此滑块现在需要将$ event对象传递给组件函数。

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

的HTML

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

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() {
  }

}
Run Code Online (Sandbox Code Playgroud)

我对Angular很陌生,在获取滑块的当前值时遇到麻烦。它似乎不适用于$ event对象?

Dav*_*983 7

我对$ 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>
Run Code Online (Sandbox Code Playgroud)

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
    );
  }

}
Run Code Online (Sandbox Code Playgroud)


Hie*_*yen 5

当您单击 mat-slider 外部的 (input) 事件时,它仍会触发此事件。

我把它改成了 (change) 事件。

这是我展示 mat-slider 价值的方式,我为谁担心。

TS文件

export class SliderOverviewExample {
  gridsize: number = 30;
  updateSetting(event) {
    this.gridsize = event.value;
  }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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