4

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

}
5
  • Can you close all input tags properly Commented Dec 14, 2018 at 6:47
  • remove @input. replace private with public. Commented Dec 14, 2018 at 6:47
  • any class member which is used outside of class should have to be public. Commented Dec 14, 2018 at 6:49
  • Closing the input tags properly broke the whole thing Commented Dec 14, 2018 at 8:14
  • Using public also does not work Commented Dec 14, 2018 at 8:14

4 Answers 4

4

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

    }
Sign up to request clarification or add additional context in comments.

2 Comments

Did that, but does not seem to help
An input can never be private due to that the other component need to access it. Some clarification :) Some people will not upvote cause you put just the answer inside and no explanation why.
1

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

1 Comment

Yes it works when used as a normal HTML file, but not when used with as an angular template
1

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.

Comments

0

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

  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.