gpt4 book ai didi

angular - 如何获取 Angular Material slider 的当前值?

转载 作者:行者123 更新时间:2023-12-02 13:08:26 25 4
gpt4 key购买 nike

My question is different to Get mdslider value in angular 2? in that I needed to pass the value of the slider to the component, not just use a local variable; but also in respect of Angular Material having changed so that the slider now requires an $event object to be passed to the component function.

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

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 很陌生,无法获取 slider 的当前值。它似乎不适用于 $event 对象?

最佳答案

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

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

}

关于angular - 如何获取 Angular Material slider 的当前值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46959135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com