6

I am trying to implement a google recapcha, I am able to verify user is Human with the help of it,

The reCapcha code is calling Callback function named 'verifyCallback'in my code, Further I want to call an AngularJS fucntion written in my controller scope.

Here are my codes so far -

Main Html , I've included-

<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>

Html partial -

    var onloadCallback = function() 
    {
        grecaptcha.render('loginCapcha', {
            'sitekey' : 'someKey',
            'callback' : verifyCallback,
            'theme':'dark'

        });
    };

    var verifyCallback = function(response) 
    {
        //I get a response if user is able to solve the Capcha challenge
        console.log(response);

        //I want to call the function called 'auth' written in my AngularJS controller
        var scope = angular.element(document.getElementById('#loginCapcha')).scope();
        scope.auth();
    };

    <div id="loginCapcha"></div>

AngularJS Controller -

var _myApp = angular.module('homeApp',[]);

_myApp.controller('loginController',['$scope',
 function($scope){

    $scope.auth = function()
    {
        console.log("Auth called");
    }
}]);
2
  • I think a best solution is to use the recaptcha JS API directly instead of doing the generic setup here. There are a few recaptcha directives if you search around but not sure which one is most up to date and/or bug free at this point. developers.google.com/recaptcha/docs/display#js_api here's one vividcortex.github.io/angular-recaptcha Commented Apr 29, 2016 at 5:01
  • 1
    @shaun thanks for the links, I was trying to implement a basic working code, I'll Look into your given links. Commented Apr 29, 2016 at 6:25

1 Answer 1

6
<div ng-controller='loginController' id='yourControllerElementID'> 

</div>

For above scenario, use following code:

var scope = angular.element(document.getElementById('yourControllerElementID')).scope();
scope.auth();

So, your method will look like this:

var verifyCallback = function(response) 
    {
        //I get a response if user is able to solve the Capcha challenge
        console.log(response);

        //I want to call the function called 'auth' written in my AngularJS controller
        var scope = angular.element(document.getElementById('#yourControllerElementID')).scope();
        scope.auth();
    };
Sign up to request clarification or add additional context in comments.

6 Comments

'yourControllerElementID' is my controller name or the function name inside my controller?
@AniruddhaRaje Updated answer, please check.
I edited the code, it's giving me a - Uncaught ReferenceError: $scope is not defined
One more thing, I don't see any controller in your div. <div id="loginCapcha"></div> should be something like <div ng-controller='loginController' id="loginCapcha"></div>.
I am using ng-route , so controller is applied from there, and ng-app is applied on main html.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.