Using AngularJS by invoking it within a <script> element

Presenting the following directive:

function Recaptcha(RecaptchaService) {
    return {
        restrict: 'E',
        templateUrl: 'app/components/login/recaptcha.html',
        controller: function($scope) {
            $scope.sendResp = function(response) {
                RecaptchaService.sendResponse(response);
            };
        }
    }
}

Utilizing the template login/recaptcha :

<script src='https://www.google.com/recaptcha/api.js'></script>

<script type="text/javascript">
    var verifyCallback = function(response) {
        $scope.sendResp(response);
    };
</script>

<div class="g-recaptcha" data-sitekey="MY-KEY" data-callback="verifyCallback"></div>

My goal is to invoke $scope.sendResp within my <script>. However, I am encountering an error stating $scope is not defined. Is there a method to achieve this?

Answer №1

If you encounter issues with captcha in ng-app, consider using the following solution:

<script type="text/javascript">
    var responseHandler = function(result) {
        var element = document.querySelector('.g-recaptcha'),
            angularElement = angular.element(element),
            scope = angularElement.scope();

            if (scope) {
                scope.sendResponse(result);
            }
    };
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The issue occurs when the AngularJS model is not updated upon selecting a date with bootstrap-datepicker

Currently, I am incorporating AngularJS with a bootstrap-datepicker plugin found here: This specific Datepicker for Bootstrap is version 1.6.4 and can be found at https://github.com/eternicode/bootstrap-datepicker. Credits go to Stefan Petre (2012) and An ...

I am interested in obtaining every separate return value from the reduce() function instead of just the final total

initialValue currentValue position elements final value first calculation 0 1 1 [0, 1, 2, 3, 4] 1 second run 1 2 2 [0, 1, 2, 3, 4] 3 third round 3 3 ...

Solving the issue of redirecting with while-else loop in Express.js

app.post('/process', function(request, response){ var i = 0; while(i < data.length){ if(data[i].condition1 == condition1 && data[i].condition2 == condition2){ response.redirect('/first_page'); ...

Retrieve the intrinsic height of an element using AngularJS

I'm trying to access the naturalHeight of an image using a directive I created: angular.module("Discavo").directive("imagecrop", function() { return { restrict: "C", link: function(scope, elem, attrs) { console.log(angular.element(ele ...

Step-by-step guide to sending RESTful requests using Angular's $http service

Currently, I am working with Nodejs and my route has this structure: router.get("/profil/:id",function (req,res) { }); I am looking to push data from my angular controller using the $http service. ...

Iterate through a listbox and append the content to a textbox using JavaScript

I am currently working on a script to read all the data from a listbox and populate a textbox with those values. I initially attempted to use a for loop for this task, but it seems to be running only once and then stopping. var listbox = $('#<%=l ...

When the <button> element associated with the JavaScript code is clicked, no result is

The code below is causing me some trouble; I can't seem to find the error in it. <!DOCTYPE html> <html> <head> <title>Party Sequence</title> </head> <body> <label for="inputN">Enter the valu ...

What is the process for importing a submodule in webpack?

I've set up a React component in an npm package called 'share-sheet'. This component is managed by webpack with the following configuration: webpack.config.js ... output: { path: path.join(__dirname, '/dist'), filename: & ...

What is the best way to adjust the fill animation of an inline svg based on the movement of the mouse within the parent div?

Can the fill of an inline SVG path be animated to change as the mouse moves across the page? ...

What is the best way to establish communication between methods in a React application?

In the SelectedTopicPage component, I currently have two methods: navigateNext and render. My goal is to pass the value of topicPageNo from the navigateNext method to the render method. What is the best way to achieve this in React? When I attempt to decla ...

Utilizing RJS and Observer to Toggle Visibility of a DIV

In my RJS template, I have a process that includes: 1. Fading out a submitted form 2. Showing a response popup div 3. Bringing up a new form using a partial My goal is to delay the display of the second form until the user clicks the "x" button on the po ...

The functionality of Jquery radio:checked is not behaving as desired

Currently, I am in the process of learning jquery. I have created a basic html file with some jquery validations, however, they are not functioning as expected. The main problem is: If I input my name first and then check the checkbox, everything works co ...

Tips for ensuring that your modal functions properly with an image tag within a figure tag

I'm facing an issue with getting a modal to display on my image gallery. The problem arises when the images are enclosed within figure tags, necessary for my hover effect, causing the modal to malfunction. I suspect that the problem lies within the f ...

What's the best way to switch between grid and list view in Vue.js?

isGridView: true, isListView: true, methods: { switchView: function() { this.isGridView = !this.isGridView; }, switchData: function () { this. ...

The implementation of local JSON instead of external JSONP in Angular

I am exploring the option of storing a json-file on the same server as my Angular app. I am wondering about how I can modify this code to read from a locally stored json file: ergastAPI.getDrivers = function() { return $http({ method: 'GET&apos ...

Fixing the PHP AJAX issue with loading additional content

Hello, I've created an app where users can click a "Load More" button to display data from a database. However, the problem arises when all the data has been fetched and the button continues to appear. If the user clicks the button again, the same dat ...

JavaScript dropdown condition explained

I have a dropdown menu that the client wants to throw an error if the value "Fail" is selected. I jokingly suggested removing "Fail" from the options, but unfortunately, he insists on keeping both "Pass" and "Fail" with an error message appearing if "Fail" ...

JavaScript - Issue with the OnClick event not triggering

I encountered an issue while trying to change the class of an element using pure Javascript upon clicking it. Even though I have used similar code successfully in the past, this particular instance did not work as expected. Here is the relevant part of the ...

Store the checkbox values until the submit button is clicked

I am in need of creating a question paper with true/false answers. Each question should have 5 answer choices - such as a, b, c, d, and e. Before submitting, the student should click the checkbox for each answer. Upon clicking the submit button, I want to ...

Can JavaScript be utilized to dynamically adjust the size of all elements on the screen to a specified percentage of their initial height and width when a certain event occurs?

I'm fairly new to the world of JavaScript, but I have a basic understanding of it. I want to optimize my personal website for mobile devices. I've already taken care of screen orientation and element positioning, everything is centered nicely and ...