landers.angular
Version:
landers.angular
35 lines (31 loc) • 1.64 kB
JavaScript
;angular.module('Landers.angular')
.directive('money', ['Flat', '$compile', '$filter', '$rootScope', function(Flat, $compile, $filter, $rootScope){
return {
restrict : 'E',
scope: {
amount: '=',
},
link : function($scope, $ele, $attrs) {
var ui_sref = $attrs['uiSref'];
var unit = $attrs['unit'] || '';
var currency = $attrs['currency'] || $rootScope.Env.currency; // || 'CNY';
if ($scope.amount) {
$scope.amount = parseFloat(parseFloat($scope.amount).toFixed(2));
}
$scope.$watch('amount', function(new_value){
Flat.set($scope, 'money.unit', unit );
Flat.set($scope, 'money.a', !!ui_sref);
Flat.set($scope, 'money.span', !ui_sref);
var if_class = "{'" + $attrs['class'] + "': amount>=0}";
var html = '';
html += '<span>';
html += ' <em class="' + currency + '" ng-show="money.span"></em>';
html += ' <span class="amount" ng-if="money.a" ng-class="' + if_class+ '" ng-bind-html="amount"></span>';
html += ' <span class="amount" ng-if="money.span" ng-class="' + if_class+ '" ng-bind-html="amount"></span>';
html += ' <em class="unit-cny" ng-show="money.unit">{{money.unit}}</em>';
html += '</span>';
$ele.replaceWith($compile(html)($scope));
});
}
};
}]);