-
-
Save surjikal/3e65bded9716eb627deb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('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); | |
}, | |
controller: function($scope, $element) { | |
} | |
}; | |
}); | |
app.value('converters', { | |
c2f: { | |
formatter: function(c) { | |
return 9 / 5 * parseFloat(c) + 32; | |
}, | |
parser: function(f) { | |
return 5 / 9 * (f - 32); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html ng-app="converter"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Temperature Converter</title> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> | |
<script src="converter.js"></script> | |
</head> | |
<body> | |
<div> | |
<input type="number" ng-model="a">℃ ⟷ | |
<input type="number" ng-model="a" converter="c2f">℉ | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment