anushka
anushka

Reputation: 345

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

}

Upvotes: 4

Views: 3774

Answers (4)

azizkale
azizkale

Reputation: 77

I solved it by adding 'any' type in addition to 'number' in myComponent.component.ts file (at param2)

    myFunction(param1: string, param2: any | number, param3: string) {

  }

Upvotes: 0

anushka
anushka

Reputation: 345

So this was a really beginner mistake. The pomodoro component was apparently getting displayed over another component and that is why the input fields weren't working. I changed the position using css and now it works.

Upvotes: 1

Amita Mohite
Amita Mohite

Reputation: 75

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

Upvotes: 1

Praveen Gupta
Praveen Gupta

Reputation: 246

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

    }

Upvotes: 4

Related Questions