Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input type="number" not working in angular form

I have the following code

<div class="container">
<form>
    <div class="form-group">
        <input type="number" class="form-control" id="hours" name="hours" 
                [(ngModel)]="hours">

                {{hours}}
        <label for="hours">hr</label>
    </div>

    <div class="form-group">
        <input type="number" class="form-control" id="minutes" name="minutes" 
                [(ngModel)]="minutes">

                {{minutes}}
        <label for="minutes">min</label>
    </div>

    <div class="form-group">
        <input type="number" class="form-control" id="seconds" name="seconds" 
                [(ngModel)]="seconds">

                {{seconds}}
        <label for="seconds">sec</label>
    </div>
</form>

For some reason, only the last input is working, and both hour and minutes are not. I checked just the html file, and that seems to work, and I am able to input in all the fields. But not when using angular.

Angular component class

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-pomodoro',
  templateUrl: './pomodoro.component.html',
  styleUrls: ['./pomodoro.component.css']
})
export class PomodoroComponent implements OnInit {
  @Input() private hours: Number;
  @Input() private minutes: Number;
  @Input() private seconds: Number;

  constructor() {

  }

  ngOnInit() {
  }

}
like image 499
anushka Avatar asked Nov 01 '25 07:11

anushka


2 Answers

Remove private from @Input() property declaration.

    import { Component, OnInit, Input } from '@angular/core';

    @Component({
      selector: 'app-pomodoro',
      templateUrl: './pomodoro.component.html',
      styleUrls: ['./pomodoro.component.css']
    })
    export class PomodoroComponent implements OnInit {
      @Input() hours: Number;
      @Input() minutes: Number;
      @Input() seconds: Number;

      constructor() {

      }

      ngOnInit() {
      }

    }
like image 66
Praveen Gupta Avatar answered Nov 03 '25 23:11

Praveen Gupta


What do you exactly mean by not working ? I just tried executing your code in fiddle. It works fine. In chrome it doesn't accept characters. In firefox it does accept characters, but flags it as invalid. https://github.com/angular/material2/issues/4941

like image 21
Amita Mohite Avatar answered Nov 03 '25 21:11

Amita Mohite