Skip to content

Commit ccd4d7c

Browse files
committed
Add dynamic script loader service
1 parent bd309a6 commit ccd4d7c

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/angular-no-captcha.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
'use strict';
22

33
angular.module('noCAPTCHA', [])
4+
.service('googleGrecaptcha', ['$q', '$window', function GoogleGrecaptchaService($q, $window) {
5+
var deferred = $q.defer();
6+
7+
$window.recaptchaOnloadCallback = function () {
8+
deferred.resolve();
9+
};
10+
11+
var s = document.createElement('script');
12+
s.src = 'https://www.google.com/recaptcha/api.js?onload=recaptchaOnloadCallback&render=explicit';
13+
document.body.appendChild(s);
14+
15+
return deferred.promise;
16+
}])
417
.provider('noCAPTCHA', function NoCaptchaProvider() {
518
var siteKey,
619
theme;
@@ -20,7 +33,7 @@ angular.module('noCAPTCHA', [])
2033
}
2134
}];
2235
})
23-
.directive('noCaptcha', ['noCAPTCHA', function(noCaptcha){
36+
.directive('noCaptcha', ['noCAPTCHA','googleGrecaptcha', function(noCaptcha, googleGrecaptcha){
2437
return {
2538
restrict:'EA',
2639
scope: {
@@ -49,15 +62,16 @@ angular.module('noCAPTCHA', [])
4962
throw new Error('Site Key is required');
5063
}
5164

52-
widgetId = grecaptcha.render(
53-
element[0],
54-
grecaptchaCreateParameters
55-
);
56-
57-
control.reset = function(){
58-
grecaptcha.reset(widgetId);
59-
scope.gRecaptchaResponse = null;
60-
};
65+
googleGrecaptcha.then(function(){
66+
widgetId = grecaptcha.render(
67+
element[0],
68+
grecaptchaCreateParameters
69+
);
70+
control.reset = function(){
71+
grecaptcha.reset(widgetId);
72+
scope.gRecaptchaResponse = null;
73+
};
74+
});
6175

6276
scope.$on('$destroy', function(){
6377
grecaptcha.reset(widgetId);

0 commit comments

Comments
 (0)