Issue with Angular: Undefined Variable

After starting to work with Angular Routes recently, I encountered a situation where data was getting lost on refresh. The solution suggested to me was to use resolve, which seemed promising but I am struggling to implement it successfully.

.config(['$routeProvider', function($routeProvider) {
           //determines redirects go via shortcuts, clicking on the management icon on the main page sends the routeProvider /MG which it then uses to pull the relevant HTML file
           $routeProvider
               .when('/MG', {
                   controller: 'teammateController',
                   templateUrl: './assets/departments/department.html',
                   resolve: {
                       activeDepartment: function() {
                           return 'Management'
                       }
                   }
               })
               .when('/PO', {
                   controller: 'teammateController',
                   templateUrl: './assets/departments/department.html',
                   resolve: {
                       activeDepartment: function() {
                           return 'Programs Office'
                       }
                   }
               })
               .when('/SM', {
                   controller: 'teammateController',
                   templateUrl: './assets/departments/department.html',
                   resolve: {
                       activeDepartment: function() {
                           return 'Sales And Marketing'
                       }
                   }
               })
               .when('/', {
                   controller: 'teammateController',
                   templateUrl: 'home.html',
                   resolve: {
                       activeDepartment: function() {
                           console.log("working");
                           return 'home';
                       }
                   }
               })
               .otherwise({
                   controller: 'teammateController',
                   templateUrl: 'home.html',
                   resolve: {
                       activeDepartment: function() {
                           console.log("working");
                           return 'home';
                       }
                   }
               })
       }]);

   app.controller('teammateController', function teammateController($scope, $http, $rootScope, $timeout, Idle, $location) {
       $scope.deptName = activeDepartment();
     });

I am encountering the error "ReferenceError: activeDepartment is not defined". Despite seeing "working" in the console log, indicating that resolve is being executed, I cannot seem to figure out why this error is occurring. Any insights or guidance would be greatly appreciated.

Answer №1

Inject the resolved properties directly into your controller for better results:

app.controller('teammateController', function teammateController($scope, $http, $rootScope, $timeout, Idle, $location, activeDepartment) {
   $scope.deptName = activeDepartment;
 });

Make sure to include the additional argument at the end of your controller.

Answer №2

app.controller('teammateController', function teammateController($scope, $http, $rootScope, $timeout, Idle, $location) {
       $scope.department = getActiveDepartment();
     });

needs to be

app.controller('teammateController', function teammateController($scope, $http, $rootScope, $timeout, Idle, $location, getActiveDepartment) {
       $scope.department = getActiveDepartment;
     });

By the way, the current syntax won't work if you plan to minify your code.

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

ag-Grid open-source server-side filtering

Currently, I am utilizing the ag-grid community version for my project. I am inquiring whether it includes support for server side filtering and paging. If not, what alternative methods can be used to implement filtering with the ag-grid community versio ...

What is the best way to prevent jest.mock from being hoisted and only use it in a single jest unit test?

My goal is to create a mock import that will be used only in one specific jest unit test, but I am encountering some challenges. Below is the mock that I want to be restricted to just one test: jest.mock("@components/components-chat-dialog", () ...

Utilizing ReactJS to creatively overlay a video on top of an image with the option to

Is there a method to superimpose a video onto an image while having the ability to select the blending mode for them? Similar to how Photoshop and Final Cut provide various blending modes such as Overlay, Multiply, Add, etc. Could there possibly be a libr ...

The issue persists as the ng-change directive fails to function despite the presence of ng-model in AngularJS

Greetings everyone. Despite searching online for a solution to my issue, I have been unable to resolve it. Below is the HTML5 code snippet I am working with: <!DOCTYPE html> <html> <head> </head> <body ng-app="myApp ...

What is the best way to ensure jQuery loads before CSS code on a website

I am currently working on a blog project that allows visitors to customize the background color by simply clicking on one of five available buttons. $(function(){ //Checking if a color scheme has already been selected var chosenColor = ...

Preventing a draggable item from being dropped into a sortable container when the maximum count has been reached

Check out the following code snippet: <link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.0/jquer ...

Accurate linking to the interface while retrieving information from a specified URL

Just started with Angular and attempting to assign the returned json data to my interface, but it's not working as expected. Check out the code I'm using below: Stackblitz Json URL ...

JavaScript: an array of objects contained within another array of objects

Here is an example dataset for reference: var info = [ {row: 0, col: 0, value: [{x: 1, y: 19}, {x: 2, y: 20}]}, {row: 0, col: 1, value: [{x: 1, y: 24}, {x: 2, y: 27}]}, {row: 1, col: 1, value: [{x: 1, y: 31}, {x: 2, y: 26}]}, {row: 1, col ...

Using a conditional statement in a JavaScript loop

Here is the data I have: companyList: [ { company_name: 'company1', item: 'item1' }, { company_name: 'company1', item: 'item2' }, { company_n ...

The process of sending an array of objects to a Controller Action through a jQuery ajax request in .NET Core 2.2 fails to function as intended

When I stringify an array of objects, the controller action does not receive it and the parameter of the action (the model) is null. I have explored various solutions to this issue on StackOverflow, but none of them have resolved my problem. Here are the ...

Receiving a console notification about a source map error

Recently, I started receiving this warning in my console: "Source map error: request failed with status 404" resource URL: map resource URL: shvl.es.js.map" Has anyone encountered this issue before? I'm unsure of what it might be? This is my webpa ...

What is the method for organizing object bodies (parent/child) in Cannon.js?

After extensively searching and reviewing issues and documentation, it seems that apart from establishing a constraint between two Cannon bodies, there is no direct method to unite shapes of varying masses. Currently, I am using the lockConstraint approac ...

Using a PHP WordPress Loop, eliminate the comma following the final object in the Schema array

My Google Schema Markup for a "Product" includes a loop that displays "Reviews". Below is an excerpt of the code: "review": [ <?php $args = array( 'post_type' => 'my_reviews', & ...

Why am I unable to utilize an array in this manner in JavaScript, and what is the method for accessing the array using a calculated number?

var nodesXY = ['15% 15%','30% 16%','19% 42%','39% 80%',]; var number = ["0","1","2","3","4","0","0","0"]; //some loop AccesNodes(number[1]); function AccesNodes(number){ console.log(number); // ...

Having trouble importing a package into my React boilerplate

Having trouble importing the react-image-crop package with yarn and integrating it into a react boilerplate. Encountered an error after installing the package: Module parse failed: /Users/...../frontend/node_modules/react-image-crop/lib/ReactCrop.js Unex ...

What makes using the `@input` decorator more advantageous compared to the usage of `inputs:[]`

In defining an input on a component, there are two available methods: @Component({ inputs: ['displayEntriesCount'], ... }) export class MyTable implements OnInit { displayEntriesCount: number; Alternatively, it can be done like this ...

Transform the Node.js requests for both GET and POST methods to utilize Ajax

I am new to JavaScript and am currently learning how to send requests from a Node.js backend using Ajax for GET/POST operations. My backend is connected to a MySQL database and I have been studying some tutorials to gain a better understanding. Here is an ...

Leveraging keypress() for managing all Vue interactions

I am in the process of converting a game from Javascript/jQuery to Vue. This particular game is entirely controlled by keyboard input, with no mouse interaction involved. Players navigate through the game using the "up" and "down" arrow keys to cycle thro ...

Using JavaScript to shift an image sideways upon clicking a hyperlink on the page

One of my clients has a unique request for an image to move across the page from left to right when a link is clicked. I'm not very experienced with javascript, so I would really appreciate any guidance on how to make this happen. Thank you in advanc ...

Utilize the native HTML attribute to capture the mouse wheel event

I'm interested in utilizing the mousewheel event in my project, but all the information I've found online relies on addEventListener(). I want to detect it using native HTML and CSS. In simpler terms, I'm hoping for something along the lines ...