Skip to content

Commit 6704e3e

Browse files
committed
[Issue-79] Angular directive: part 2
1 parent c9beb09 commit 6704e3e

File tree

10 files changed

+210
-16
lines changed

10 files changed

+210
-16
lines changed

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Cleave.js has a simple purpose: to help you format input text content automatica
1717
- Custom delimiter, prefix and blocks pattern
1818
- CommonJS / AMD mode
1919
- ReactJS component
20+
- AngularJS directive (1.x)
2021

2122
**TL;DR** [the demo page](http://nosir.github.io/cleave.js/)
2223

@@ -144,11 +145,55 @@ As you can see, here you simply use `<Cleave/>` as a normal `<input/>` field
144145

145146
Usage for `Webpack`, `Browserify` and more in documentation: [ReactJS component usage](https://github.com/nosir/cleave.js/blob/master/doc/reactjs-component-usage.md)
146147

148+
## AngularJS directive usage
149+
150+
First include the directive module:
151+
152+
```html
153+
<script src="cleave-angular.min.js"></script>
154+
<script src="cleave-phone.{country}.js"></script>
155+
```
156+
157+
And in the model:
158+
159+
```js
160+
angular.module('app', ['cleave.js'])
161+
162+
.controller('AppController', function($scope) {
163+
$scope.onCreditCardTypeChanged = function(type) {
164+
$scope.model.creditCardType = type;
165+
};
166+
167+
$scope.model = {
168+
value: ''
169+
};
170+
171+
$scope.options = {
172+
creditCard: {
173+
creditCard: true,
174+
onCreditCardTypeChanged: $scope.onCreditCardTypeChanged
175+
}
176+
};
177+
});
178+
```
179+
180+
Then easily you can apply `cleave` directive with `input` field:
181+
182+
```html
183+
<div ng-controller="AppController">
184+
<input ng-model="model.value" ng-whatever="..." type="text" placeholder="Enter credit card number"
185+
cleave options="options.creditCard"/>
186+
</div>
187+
```
188+
189+
More usage in documentation: [Angular directive usage](https://github.com/nosir/cleave.js/blob/master/doc/angularjs-directive-usage.md)
190+
147191
## Playground
148192

149193
- [Plain JSFiddle (Basic usage)](https://jsfiddle.net/nosir/kbaxx64s/)
150194
- [Plain JSFiddle (More examples)](https://jsfiddle.net/nosir/aLnhdf3z/)
151195
- [React JSFiddle](https://jsfiddle.net/nosir/gLLsrxxf/)
196+
- [Angular JSFiddle](https://jsfiddle.net/nosir/q58sh22t/)
152197

153198
## Documentation
154199

@@ -158,6 +203,7 @@ Usage for `Webpack`, `Browserify` and more in documentation: [ReactJS component
158203
- [Public methods](https://github.com/nosir/cleave.js/blob/master/doc/public-methods.md)
159204
- [Phone lib addon](https://github.com/nosir/cleave.js/blob/master/doc/phone-lib-addon.md)
160205
- [ReactJS component usage](https://github.com/nosir/cleave.js/blob/master/doc/reactjs-component-usage.md)
206+
- [AngularJS directive usage](https://github.com/nosir/cleave.js/blob/master/doc/angularjs-directive-usage.md)
161207

162208
## Building & Running tests
163209

@@ -182,7 +228,7 @@ gulp mocha && gulp eslint
182228
- [x] Add credit card type detection callback
183229
- [x] Mocha unit tests for formatter
184230
- [ ] Fix the classic cursor jumping issue
185-
- [ ] AngularJS component (WIP...)
231+
- [x] AngularJS directive (1.x)
186232
- [ ] PhantomJS / Jest browser tests
187233

188234
> For contributors, we have a [not in the plan](https://github.com/nosir/cleave.js/blob/master/doc/not-in-the-plan.md) list you may concern.

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"form",
99
"input"
1010
],
11-
"version": "0.6.7",
11+
"version": "0.6.9",
1212
"author": {
1313
"name": "Max Huang",
1414
"email": "[email protected]",

dist/cleave-angular.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,13 +816,26 @@ angular.module('cleave.js', [])
816816
.directive('cleave', function () {
817817
return {
818818
restrict: 'A',
819+
require: 'ngModel',
819820

820821
scope: {
821-
options: '='
822+
options: '=',
823+
onValueChange: '&?'
822824
},
823825

824826
controller: function ($scope, $element) {
825-
new Cleave($element[0], $scope.options);
827+
$scope.cleave = new Cleave($element[0], $scope.options);
828+
$scope.onValueChange = $scope.onValueChange || null;
829+
},
830+
831+
link: function ($scope, $element, attrs, ngModel) {
832+
if ($scope.onValueChange) {
833+
$scope.$watch(function () {
834+
return ngModel.$modelValue;
835+
}, function () {
836+
$scope.onValueChange()($scope.cleave.getFormattedValue(), $scope.cleave.getRawValue());
837+
});
838+
}
826839
}
827840
};
828841
});

dist/cleave-angular.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cleave-react.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cleave.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/angularjs-directive-usage.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Cleave.js Documentation
2+
3+
[Documentation](https://github.com/nosir/cleave.js/blob/master/doc/doc.md) > AngularJS directive usage
4+
5+
## Playground
6+
7+
- [Angular JSFiddle](https://jsfiddle.net/nosir/q58sh22t/)
8+
9+
## Basic usage
10+
11+
First include the directive module:
12+
13+
```html
14+
<script src="cleave-angular.min.js"></script>
15+
<script src="cleave-phone.{country}.js"></script>
16+
```
17+
18+
And in your model:
19+
20+
```js
21+
angular.module('app', ['cleave.js'])
22+
23+
.controller('AppController', function($scope) {
24+
$scope.onCreditCardTypeChanged = function(type) {
25+
$scope.model.creditCardType = type;
26+
};
27+
28+
$scope.model = {
29+
value: ''
30+
};
31+
32+
$scope.options = {
33+
creditCard: {
34+
creditCard: true,
35+
onCreditCardTypeChanged: $scope.onCreditCardTypeChanged
36+
}
37+
};
38+
});
39+
```
40+
41+
Then you can just use `cleave` directive with `input` field:
42+
43+
```html
44+
<div ng-controller="AppController">
45+
<input ng-model="model.value" ng-whatever="..." type="text" placeholder="Enter credit card number"
46+
cleave options="options.creditCard"/>
47+
</div>
48+
```
49+
50+
## Advanced usage
51+
52+
Sometimes you might want to get the raw value. Here is the way to deal with it:
53+
54+
First in you model:
55+
56+
```js
57+
angular.module('app', ['cleave.js'])
58+
59+
.controller('AppController', function($scope) {
60+
$scope.onCleaveValueChange = function(formattedValue, rawValue) {
61+
$scope.model.formattedValue = formattedValue;
62+
$scope.model.rawValue = rawValue;
63+
};
64+
65+
$scope.onCreditCardTypeChanged = function(type) {
66+
$scope.model.creditCardType = type;
67+
};
68+
69+
$scope.model = {
70+
value: ''
71+
};
72+
73+
$scope.options = {
74+
creditCard: {
75+
creditCard: true,
76+
onCreditCardTypeChanged: $scope.onCreditCardTypeChanged
77+
}
78+
};
79+
});
80+
```
81+
82+
Then in your html:
83+
84+
```html
85+
<div ng-controller="AppController">
86+
<input ng-model="model.value" ng-whatever="..." type="text" placeholder="Enter credit card number"
87+
cleave options="options.creditCard" on-value-change="onCleaveValueChange"/>
88+
89+
<p>raw value: {{model.rawValue}}</p>
90+
<p>formatted value: {{model.formattedValue}}</p>
91+
92+
<p>ng-model value: {{model.value}}</p>
93+
94+
<p>type: {{model.creditCardType}}</p>
95+
</div>
96+
```
97+
98+
As you can see, by passing the function (without `()`) to `on-value-change`, you register a callback from `cleave.js` directive.
99+
100+
Then in the callback, it returns `formattedValue` and `rawValue`. `formattedValue` here is same as your `ng-model` value.

gulp-tasks/build.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,22 @@ var paths = {
2222
dist: './dist'
2323
};
2424

25-
gulp.task('min', function () {
25+
gulp.task('min-mangle', function () {
2626
return gulp.src([
2727
path.join(paths.dist, 'cleave.js'),
28-
path.join(paths.dist, 'cleave-react.js'),
28+
path.join(paths.dist, 'cleave-react.js')
29+
])
30+
.pipe(uglify({mangle: true}))
31+
.pipe(header(getLicense(), {
32+
version: packageInfo.version,
33+
build: (new Date()).toUTCString()
34+
}))
35+
.pipe(rename({suffix: '.min'}))
36+
.pipe(gulp.dest(path.join(paths.dist)));
37+
});
38+
39+
gulp.task('min-no-mangle', function () {
40+
return gulp.src([
2941
path.join(paths.dist, 'cleave-angular.js')
3042
])
3143
.pipe(uglify({mangle: false}))
@@ -60,4 +72,14 @@ gulp.task('js:angular', function () {
6072
.pipe(gulp.dest(paths.dist));
6173
});
6274

63-
gulp.task('build', gulpsync.sync(['js', 'js:react', 'js:angular', 'min']));
75+
gulp.task('build', gulpsync.sync([
76+
// sync
77+
'js',
78+
'js:react',
79+
'js:angular',
80+
[
81+
// async
82+
'min-mangle',
83+
'min-no-mangle'
84+
]
85+
]));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"form",
1111
"input"
1212
],
13-
"version": "0.6.7",
13+
"version": "0.6.9",
1414
"author": {
1515
"name": "Max Huang",
1616
"url": "http://github.com/nosir",

src/Cleave.angular.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@ angular.module('cleave.js', [])
22
.directive('cleave', function () {
33
return {
44
restrict: 'A',
5+
require: 'ngModel',
56

67
scope: {
7-
options: '='
8+
options: '=',
9+
onValueChange: '&?'
810
},
911

1012
controller: function ($scope, $element) {
11-
new Cleave($element[0], $scope.options);
13+
$scope.cleave = new Cleave($element[0], $scope.options);
14+
$scope.onValueChange = $scope.onValueChange || null;
15+
},
16+
17+
link: function ($scope, $element, attrs, ngModel) {
18+
if ($scope.onValueChange) {
19+
$scope.$watch(function () {
20+
return ngModel.$modelValue;
21+
}, function () {
22+
$scope.onValueChange()($scope.cleave.getFormattedValue(), $scope.cleave.getRawValue());
23+
});
24+
}
1225
}
1326
};
1427
});

0 commit comments

Comments
 (0)