Assigning a value to a variable outside of a function in AngularJS using $on: Is it possible?

Before calling $on, I need to assign the value of

$scope.attestorObj.riskAssessmentRoleAsgnKey
to a global variable called roleAsgnKy. I am new to angularJS and would appreciate any help on how to achieve this.

This is what I have tried so far...

In main.js:

var roleAsgnKy;
              $scope.filesGridOptions = attestorConfig.getAttestorHistoryFile();
              $scope.filesGridOptions.dataSourceattestorFactory.getFilesHistoryDataSource(roleAsgnKy);

    $scope.$on('addEditAttest',function(s,attestorObj){
               $scope.attestorObj = attestorObj;
               $scope.attestorObj.riskAssessmentRoleAsgnKey = roleAsgnKy;
             });

Answer №1

After considering your comment regarding the desire for a global variable, there are two possible solutions to choose from:

1) Implement a service or factory to manage and access that variable. This will allow you to set and retrieve it from any part of your application. Learn more about services here.

2) Alternatively, you can define the variable within $rootScope. This way, you can inject it into any controller and modify it from anywhere. See how to use $rootScope here.

It's generally recommended to employ a service for this type of situation. Additionally, you can encapsulate any necessary logic related to the variable within the service.

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 transition of the element transform between the states of 'relative' and 'fixed' lacks smoothness

On my webpage, I have a banner element with the class .banner and a floating element with the class .float. As the user scrolls, I want the floating element to appear centered vertically relative to the banner when it is in view. However, once the user sc ...

What is the method for adding pages to the ion-nav component in Ionic with Angular?

How can I implement a UINavigationController-like functionality in iOS using an ion-nav element? The example provided here is in Javascript, but I need assistance with implementing it in Angular. Specifically, I'm unsure of how to programmatically add ...

Top method for integrating switchable pages into a Bootstrap single-page application

Currently, I am in the process of developing a straightforward web UI for an embedded platform. In terms of styling the layout, I am considering using bootstrap 4 as it appears to be a suitable option. The objective is to create a one-page application with ...

Tips for troubleshooting objects within an Angular template in an HTML file

When I'm creating a template, I embed some Angular code within my HTML elements: <button id="btnMainMenu" class="button button-icon fa fa-chevron-left header-icon" ng-if="(!CoursesVm.showcheckboxes || (CoursesVm.tabSelected == 'curren ...

Using React and Material UI to hide a child element when it is hovered over with the help of withStyles

I am trying to hide the ListItemSecondaryAction element with the class actions when hovering over the ListItem with the class friendsListItem. Despite attempting to use the descendent selector within styles, I have not been able to achieve the desired res ...

Unable to retrieve res.user.username while using passport within express-session

I'm currently diving into the realm of creating sessions with Passport.js and Express.js. My goal is to retrieve the username from a user stored in a session using res.user.username, but I seem to be facing some challenges. Below is the snippet of m ...

What is preventing my application (docker) from connecting to the database?

Encountering a frustrating issue here: I've developed a NodeJS application with a server listening on port number 3000. The app includes simple operations such as post, put, and read, which interact with a database running in a Docker Container on por ...

Is there a way to adjust the width of the info panel in an SVG to automatically match the width of the SVG itself?

I am looking to place the information panel at the bottom of my SVG map and have it adjust its width according to the width specified in the viewBox. This way, even if I resize the map, the info panel will automatically adjust to fill completely based on t ...

Is there a way to overlay a div on a particular line within a p element?

Within this paragraph lies a collection of text encapsulated by a < p > tag. When this content is displayed on the page, it spans across 5 lines. My objective is to creatively style and position a < div > tag in order to highlight a specific li ...

Selenium: The ultimate guide to inserting text within a div element located inside an iframe

Snippet of HTML code: <div class="listRte__editorFrame"> <iframe src="about:blank" style="height: 150px;"> #document <html> <head> <body> ...

Angular UI-Utilities with an added highlight filter

In my treeview structure, I have implemented a search feature. Now, I am looking to add the functionality to highlight multiple search words. I have tried using Angular's ui-utils and the highlight filter, but it only works for highlighting a single w ...

What is the method for submitting a form when a change occurs?

This inquiry is focused on the Captive Network Assistant. I attempted to utilize plain JavaScript, <form action=""> <select name="test" onchange="this.form.submit()"> <option value="1">1</option> <option val ...

Oops: [ERR_HTTP_HEADERS_SENT]: Headers can't be set once they've been sent to the client, please fetch again

I'm encountering an issue with my code where I am unable to make a fetch request when invoking sendPushMessages(message) due to HTTP ERRORS, but I am unsure of the root cause. The console is displaying the following error message: Error [ERR_HTTP_HEA ...

The function is not a function: TypeError: func(...).then

I am a newbie in JavaScript, currently delving into functions and promises. I have created a small code snippet with the following purpose: to comprehend how promise function (then) works on a function that returns a value. input: a,b output: i ...

What is the best way to display a Blender model on a webpage?

After researching different methods for rendering my blender models on my web application, I have chosen to export my model in .gltf format. Here is the code I am currently using: App.js import React, { Suspense } from 'react'; import './Ap ...

Using React Router useHistory to navigate to different routes programmatically

Currently, I'm working on creating a wizard interface and running into some issues with the buttons. Right now, I have next and back buttons for each route, but I am aiming to create a button component that can handle navigation both forward and backw ...

Every time I try to restart my React Project, it seems to encounter strange issues that

Currently, I am following a fullstack React tutorial which you can find here: React Tutorial I have encountered an issue where every time I close my laptop and reopen the project, npm start throws a strange error. Initially, I tried to fix it by starting ...

Creating a running text (marquee) with CSS is a simple and efficient way to make

I encountered a significant challenge while delving into CSS animation. My goal is to create a "transform: translate" animation that displays text overflowing the content width as depicted in the image below. https://i.stack.imgur.com/sRF6C.png See it i ...

Is it possible to duplicate a response before making changes to its contents?

Imagine we are developing a response interceptor for an Angular 4 application using the HttpClient: export class MyInterceptor implements HttpInterceptor { public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<an ...

What steps do I need to take in order to successfully make a checkbox?

I am attempting to use Angularjs to set a checkbox to true. When saved, it should store an integer value (1 or 0) in my field. Below is the code snippet for the view: <input type="checkbox" ng-model="lis.SMSPromotion" id="SMSPromotion" ng-init="checke ...