here is the plunker link-> http://plnkr.co/edit/iFnjcq?p=preview
As you can see in the link, it performs validation of input field. only numbers are allowed to enter and it adds commas automatically.
My question is-> if i give a negative number in that field, how can i change the color of the value entered in that field automatically.
i.e A positive number should display as it is. when user enters negative, numbers get entered with red color of the text.
Is there any way i can achieve this guys ??
HTML-
<div ng-controller="MainCtrl">
<input type="text" ng-model="amount" format="number" />
</div>
JS directive
var app = angular.module('App',[]);
app.controller('MainCtrl', function ($scope) {
});
app.directive('format', ['$filter', function ($filter) {
return {
require: 'ngModel',
link: function (scope, elem, attrs, ctrl) {
if (!ctrl) return;
ctrl.$formatters.unshift(function (a) {
return $filter(attrs.format)(ctrl.$modelValue);
});
ctrl.$parsers.unshift(function (viewValue) {
var plainNumber = viewValue.replace(/[^\d|\-+]/g, '');
elem.val($filter('number')(plainNumber/100,2));
return plainNumber;
});
}
};
}]);
Regarding the style overwrite issue discussed below, here is the problem.
The actual HTML Code
<div class="Ip"><input type="text" ng-model="amount" format="number" /></div>
The CSS is applied through scss for the class Ip. Here it is
$font:#fff; //this font gets overwritten. even if i exclude this font of bootstrap is there which will overwite the class mentioned in directive.
.inputCl{
color:$font;
}
.Ip{
input{
@extend .inputCl;
}
}