Hey there! I've got a setup that seems to be giving me some unexpected results. Whenever I make changes to either the Kelvin or Celsius field, I end up with strange outputs like multiplying by 1000 or other inexplicable numbers. I'm new to Angular 2, so I'm thinking it might have something to do with how I set up the binding.
Can anyone lend a hand? Any help would be greatly appreciated!
class Temperature {
kelvin: number;
constructor(kelvin: number) {
this.kelvin = kelvin;
}
getKelvin(): number {
return this.kelvin;
}
setKelvin(kelvin: number) {
this.kelvin = kelvin;
}
getCelsius(): number {
return this.kelvin - 273.15;
}
setCelsius(celsius: number) {
this.kelvin = celsius + 273.15;
}
}
export class ConverterComponent implements OnInit {
temperature: Temperature;
constructor() {
this.temperature = Temperature.fromKelvin(21);
}
onKelvinChange(kelvin: number) {
this.temperature.setKelvin(kelvin);
}
onCelsiusChange(celsius: number) {
this.temperature.setCelsius(celsius);
}
<input id="temperature-kelvin" type="number" class="form-control" [ngModel]="temperature.getKelvin()" (input)="onKelvinChange($event.target.value)" />
<input id="temperature-celsius" type="number" class="form-control" [ngModel]="temperature.getCelsius()" (input)="onCelsiusChange($event.target.value)" />