Ways to trigger an AngularJS event from a popup?

Situation: I am working on an AngularJS application that utilizes Google's authentication feature.

Depending on the specific resource, when Angular communicates with my backend, it may request an Oauth token. In such cases, Angular will display a popup (not a Bootstrap modal) containing the redirect URL.

Google will authenticate the user, seek permission, and then redirect them back to my application. There is a callback URL that will handle the 'code' and obtain the actual token. This callback URL is triggered within the popup.

Once the token is acquired, the popup should automatically close and inform Angular to retry the last request (now that the token is stored in the user's session)

Issue: How can a popup trigger an event in Angular?

It doesn't necessarily have to be $rootScope.$emit, but there should be a way to notify Angular.

I came across this solution, but it may not be suitable for Angular :(

Answer №1

If you are looking to implement localStorage events, you can refer to this helpful demo:

// To implement this functionality on the main page
$window.addEventListener('storage', function(event) { 
  if(event.key === 'login-notification') {
    // Notification received!
    // You can access the value using "event.newValue"
  }
}, false);

// Trigger the event by using setItem from the popup
$window.localStorage.setItem('login-notification', 'ayy');

Answer №2

Start by creating a new service.

app.service("customService", function(){
  return {
    set : function(key, value){
          this[key] = value;    
     },
     get : function (key){
        return this[key];
     }
  }
});

Now, you can easily set the token in the service within the app, making the value accessible throughout. You can also use $watch to monitor specific keys in the service and trigger actions based on the set value.

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

Converting Numbers into Words with the power of HTML and JavaScript

CONVERT NUMBER TO WORDS Write a simple JavaScript program that converts a number to words. Include an HTML page where the user can input the number to be converted. The conversion process should involve using both for loops and switch statements. ...

Unable to create selected buttons in Vue.js

I'm having trouble creating buttons that can select all options when clicking on either size or color. The buttons aren't showing up at all. Can someone help me identify the issue? I've attempted various solutions but none seem to work. Any ...

Angular-ui typeahead feature allows users to search through a predefined

I recently developed a typeahead feature using the angular-ui library. Here is the current HTML for my typeahead: <input name="customers" id="customers" type="text" placeholder="enter a customer" ng-model="selectedCustomer" uib-typeahead="customer.fir ...

Sending a JavaScript string to a PHP script from a Chrome extension content script

I am currently developing a chrome extension that is designed to extract text data from specific websites when I visit them, and then store this data in a SQL database. The JavaScript code for data extraction is functioning correctly and is able to capture ...

Displaying and hiding the Angular <object> element

I am faced with a challenge involving managing files of various formats and creating a gallery with preview functionality. Everything works smoothly when clicking through items of the same format, like JPEGs. However, an issue arises when switching from vi ...

I'm looking to validate an input tag in HTML5 using JQuery. Can anyone help clarify this process for me?

New to JQuery and struggling with HTML5 input tag validation. On my page, I have an input tag like this: <input id="variazioneAnticipo" class="rightAlligned form-control" style="width:50%" type="number" step="0.01" /> and a button: <button id="va ...

Use of absolute positioning resulted in the disappearance of the element

Can anyone assist with resolving the issue I am encountering? I currently have three nested divs as follows: <div class="section"> <div class="parent"> <div class="child"> Some random text. </div> </div> </div> To adj ...

The issue of calling the child window function from the parent window upon clicking does not seem to be functioning properly on Safari and Chrome

I'm attempting to invoke the function of a child window from the parent window when a click event occurs. Strangely, this code works in Firefox but not in Safari or Chrome. Here is the code snippet I am using: var iframeElem = document.getElementById( ...

Looking for the properties of the ServerResponse object in node.js - where can I locate them?

Currently, I've been diving into the book Node.js In Action. Chapter 9 introduces a message module with a function that has left me puzzled about the 'this' object when calling res.message. To gain clarity, I decided to print out the name of ...

The footer is now accompanied by the <v-navigation-drawer> on the side

I am looking for a way to dynamically adjust the height value of a style applied to an element based on certain conditions. Specifically, when scrolling to the bottom, I want the height to be 77.5%, when the footer is not visible at all, it should be 100%, ...

Is there a way to prevent the jsonPayload in stackdriver from automatically converting to a struct format?

When working with a Google Cloud Function, I am logging a simple JSON structure like this: console.info(JSON.stringify({message: 'xxx', data: {status: 200}, status: 200})); However, the log appears in an unreadable format in Stackdriver. How ca ...

Is it considered inefficient to import every single one of my web components using separate <script> tags?

Currently, I am in the process of developing a website with Express + EJS where server-side rendering is crucial. To achieve this, I am utilizing web components without shadow dom. Each page type (home, post, page) has its own EJS view. For instance, when ...

Updating the parent component of a Vue router when closing nested routes

I have a scenario where I encounter the following steps: Go to the /parent page and see the Parent component being displayed Proceed to /parent/john and witness the Child component being rendered Return back to /parent and observe the child component bei ...

Adding QML code into a Jade file

Currently working on developing a straightforward video streaming application using Node.js and integrating the WebChimera plugin. The player configuration is done in QML with Chimera, and I am facing numerous errors during the compilation process in Jade. ...

Efficiently handle user authentication for various user types in express.js with the help of passport.js

Struggling to effectively manage user states using Passport.js in Express.js 4.x. I currently have three different user collections stored in my mongodb database: 1. Member (with a profile page) 2. Operator (access to a dashboard) 3. Admin (backend privi ...

Mandatory input Angularjs 1.2 and Bootstrap 3

Unable to create a form field with red highlight for required items in updated AngularJS and Bootstrap 3. Looking for solution without writing CSS, using only Bootstrap 3. Here's my setup on Plunker. UPDATE: Replaced content below with comments' ...

Creating an if statement based on the currently chosen option

My angular JS page includes a drop-down list: <div class="col-md-4 fieldMargin"> <div class="dropdownIcon"> <select name="actions" id="actions" ...

Flag a specific property on a directive scope as needing an update

Recently, I made the switch to using AngularJS in a web application that was previously mostly jQuery based. One of the components in this app utilizes jQuery UI's draggable feature, which relies on the CSS left property for positioning objects during ...

What is the best way to dismiss the ant-design-vue select option when the mouse moves away?

<a-select showSearch placeholder="Select a person" optionFilterProp="children" style="width: 200px" :open="isOpen" @mouseenter="handleOpenSelect" @mouseleave="handleCloseSelect" > <a-select-option value="jack"> ...

Empty screen appears when "npm run serve" command is executed following the build process

I am currently utilizing Material-ui. Following the project build with npm run build, I encounter a blank page when running npm run serve. I attempted to set homepage: "./" in the package.json as suggested here, however, it still displays a blank ...