angular-directive-percentage
Credit
This directive is a port of fiestah's great work on angular-money-directive. Just slightly altered for percentages
Overview
This directive takes inputs in decimal format 0.425376 and displays it in a percentage format 42.5376 (%).
It does a few things:
- Prevents entering non-numeric characters
- Prevents entering the minus sign when
min >= 0
- Supports
min
andmax
like in<input type="number">
- Rounds the model value by
precision
, e.g.0.42219
will be rounded to42.22
by default - On
blur
, the input field is auto-formatted. Say if you enter42
, it will be formatted to42.00
Usage:
$ bower install angular-directive-percentage
Then include it as a dependency in your app.
angular.module('myApp', ['angular-directive-percentage'])
Attributes:
percentage
: requiredng-model
: requiredtype
: Set totext
or just leave it out. Do not set tonumber
.min
: optional Defaults to0
.max
: optional Not enforced by defaultprecision
: optional Defaults to4
. Set to-1
to disable rounding
Basic example:
<input type="text" ng-model="model.percent" percentage>
min
, max
and precision
can be set dynamically in $scope:
<input type="text" ng-model="model.percent" percentage min="{{min}}">
Tests:
- Install test deps:
npm install
andbower install
- Run:
npm test
or./node_modules/karma/bin/karma start
.
Note: If running locally, you might get an error about can't submit to coveralls. This is only required if you want the code coverage badge for your forked repo. See below for setup.
Coveralls.io
Using coveralls.io for the code coverage badge. Using instanbul, karma-coverage, and karma-coveralls to generate code coverage and push to coveralls.io. The push to coveralls.io requires the repo token. I am using an environment variable
$ export COVERALLS_REPO_TOKEN_FOR_ANGULAR_DIRECTIVE_PERCENTAGE=[your coveralls repo token]
that gets mapped to the required COVERALLS_REPO_TOKEN per test run. The travis build sets this environment variable using encrypted variables. However, if running this locally, you will need to setup your forked repo in coveralls.io and set that environment variable to your repo token