JSFiddle - React, Tailwind, and code Playground

Angular convertor

by pablojim

HTML

<div id="tc-angular">
    <input type="number" ng-model="c">°C</input>
	<input type="number" ng-model="c" converter="c2f">°F</input>
</div>

JavaScript

function c2f(c) {
	return 9/5 * c + 32
}
function f2c(f) {
	return 5/9 * (f - 32)
}

var app = angular.module('temperature-converter', []);

app.directive('converter', function(converters) {
	return {
		require: 'ngModel',
		link: function(scope, element, attr, ngModel) {
			var converter = converters[attr.converter]
			ngModel.$formatters.unshift(converter.formatter)
			ngModel.$parsers.push(converter.parser)
			scope.c = 0
		}
	}
})

app.value('converters', {
	c2f: {
		formatter: c2f,
		parser: f2c
	}
})

angular.bootstrap(document.getElementById('tc-angular'), ['temperature-converter']);