I'm new to Angular so please excuse my neewbie questions :) I'm creating a small project to show some sport trainings data. I need to implement a simple converter for units like speed, distance etc. For now, i have created a Pipe that calculates the values and adds the correct unit. I've declared variables to determine which units to use. It looks something like this:
let speedUnit = 'kmh';
switch (args) {
case 'speed': {
if (speedUnit == 'kmh') {
newValue = value * 3.6;
newValue = newValue.toFixed(2);
newValue += ' km/h';
}
if (speedUnit == 'mph') {
newValue = value * 2.23694;
newValue = newValue.toFixed(2);
newValue += ' mph';
}
break;
}
It works great so far. The problem is, I need it more dynamic, so i can switch the unit with a click. E.g. there's would be a switch in frontend with mph/kmh on it and on click it would change the value of the speedUnit
variable from 'kmh' to 'mph' and update the on-screen data.
What would be the best approach here? Using some kind of Service? Observables? As I mentioned, im new to Angular and I'm not yet familiar with all of it's components. :)
Thank's for any help.