Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to listen to changes on an input range in Angular 2?

Tags:

angular

I'm developing an app with Angular 2.0, and I don't know how to listen to an input range changes.

Here's the component:

import { Component } from '@angular/core';

@Component({
  selector: 'zoom-slider',
  styleUrls: [
    './zoom-slider.style.scss'
  ],
  template: `
    <div class="zoom-slider">
      <input type="range" max="5" (click)="changeZoom"/>
    </div>
  `
})
export class ZoomSlider {

  changeZoom() {
    console.log('change slider');
  }

}

This doesn't work.

Thanks in advance!

like image 493
Javier Manzano Avatar asked Jul 22 '16 12:07

Javier Manzano


People also ask

How do I detect a text input change event with angular?

When we enter tags one character at a time, Angular performs change detection after every character is entered. So, if we type in “foo”, the Angular binding records for the <input> element value attribute will follow this sequence: "" , "f" , "fo" , "foo" .

How do you put a range in input?

<input type="range"> <input> elements of type range let the user specify a numeric value which must be no less than a given value, and no more than another given value. The precise value, however, is not considered important.

What is range in angular?

Angular Bootstrap 5 Range component A Range is an interactive component that lets the user swiftly slide through possible values spread over the desired range. Note: Read the API tab to find all available options and advanced customization.


2 Answers

You need to listen for the change event and have a reference to the input.

<input type="range" #ref (change)="changeZoom(ref.value)"/>

changeZoom(value: number) { ... }
like image 93
Mateusz Kocz Avatar answered Oct 05 '22 18:10

Mateusz Kocz


I guess one of these should work

<input type="range" max="5" (input)="changeZoom($event)"/>
<input type="range" max="5" (change)="changeZoom($event)"/>

If you pass the function as above (click) might work as well.

like image 24
Günter Zöchbauer Avatar answered Oct 05 '22 18:10

Günter Zöchbauer