responsive number with angularjs
http://stackoverflow.com/questions/27324144/creating-a-responsive-number-directive-with-angular-and-bootstrap
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.4/angular.js"></script>
<div ng-controller="MyCtrl">
<div class="container">
<div class="row">
<div class="col-md-4">{{myValue}}</div>
<div class="col-md-8">
<div responsivenumber ng-model="myValue">
</div>
</div>
</div>
</div>
</div>
JavaScript
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.myValue = 100000000000000000;
}
myApp.directive('responsivenumber', ['$filter', function ($filter) {
return {
require: 'ngModel',
restrict: 'A',
scope: true,
link: function (scope, element, attrs, ctrl) {
var scrollWidth = element[0].scrollWidth;
var parentWidth = getParentWidth();
var displayFriendlyValue = false;
initializeResponsiveNumber();
ctrl.$render();
ctrl.$render = function (isResizeEvent) {
var temp = shouldRenderFriendlyValue();
if(isResizeEvent){
// then the window was resized and the element is already formatted
// so if it fits it fits..
if (displayFriendlyValue == temp) {
return;
}
}
displayFriendlyValue = temp;
var viewValue = getTransformedViewValue(ctrl.$modelValue, displayFriendlyValue);
ctrl.$viewValue = viewValue;
element.html(ctrl.$viewValue);
// after we've formatted the number it may not fit anymore
// so if shouldRenderFriendlyValue() previously returned false
// that means the unformatted number was not overflowing, but
// the formatted number may overflow
if (!displayFriendlyValue) {
// we check shouldRenderFriendlyValue() again
// because sizing can change after we set
// element.html(ctrl.$viewValue);
displayFriendlyValue = shouldRenderFriendlyValue();
if (displayFriendlyValue) {
viewValue = getTransformedViewValue(ctrl.$modelValue, displayFriendlyValue);
ctrl.$viewValue = viewValue;
element.html(ctrl.$viewValue);
return;
...