Strategies for transferring data between controllers and configuration files in AngularJS

I'm attempting to pass a variable to the angular js config, Here is my JavaScript app:

var app = angular.module('myApp', ['ngMaterial', 'sasrio.angular-material-sidenav', 'ui.router']);
app.controller('baseController', function ($scope, $mdDialog, $mdMedia, $http, $rootScope, $timeout, $mdToast, $mdSidenav, ssSideNav) {
    $scope.menu = ssSideNav;
    $scope.getRequirment = function () {
        $http.get("site/requirment",
                {params: {dir: $scope.currentPath}})
                .then(function (response) {
                   return response.data;
                });
    };
    $scope.getRequirment();
});
app.config(['$mdThemingProvider', '$locationProvider', 'ssSideNavSectionsProvider', '$stateProvider',
    function ($mdThemingProvider, $locationProvider, ssSideNavSectionsProvider, $stateProvider) {
        $mdThemingProvider.theme('default')
                .primaryPalette('deep-orange')
                .accentPalette('deep-orange');
        ssSideNavSectionsProvider.initWithTheme($mdThemingProvider);
        ssSideNavSectionsProvider.initWithSections("Get getRequirment function in controller");
    }]);

How do I invoke a function in the controller from config?

What I mean is this line:

ssSideNavSectionsProvider.initWithSections("Get getRequirment function in controller");

Answer №1

If you're looking for a quick solution, you can try the following method:

Simply add this to your configuration file:

$rootScope.$watch("variable", function(newVal, oldVal) {
// Do something with the new value
})

Then in your controller, just assign a value to

$rootScope.variable

Answer №2

Trying to execute the controller during the configuration phase is not possible because controllers run in the run phase, while configurations run in the config phase.

After examining the source code a bit, I found that sections are available as a service variable. You can experiment with adding them from the controller using this line of code:

ssSideNavSections.sections.push(yourSection);

Answer №3

Including a constant in the configuration block allows for easy access to it by injecting it into the controller.

For more information, check out:

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

Tips for resolving jQuery conflict problems

I am dealing with a jQuery issue where I have two scripts - one for the slider and the other for a dropdown menu. When I remove one script, the slider works but the dropdown doesn't, and vice versa. I have looked at tutorials online on how to resolve ...

Steps for replacing the firestore document ID with user UID in a document:

I've been attempting to retrieve the user UID instead of using the automatically generated document ID in Firebase/Firestore, but I'm encountering this error: TypeError: firebase.auth(...).currentUser is null This is the content of my index.js ...

Show or hide a div through two variables that toggle between different states

It's possible that I'm not fully awake, so missing the obvious is a possibility. However, I have 2 variables that determine whether a div has a specific class or not. The class functions more like a toggle; so the following conditions should tri ...

Changing a multidimensional object array into a standard array

Hey there, I've mastered how to retrieve errors from Laravel validation, but now I'm facing a challenge in displaying them. I'm looking for a straightforward way to iterate through each error message and present it on the user's page us ...

The command "npm run build" is failing and encountering errors within the webpack project

Today, when I ran npm run build in the terminal, my project stopped running and displayed 90 errors and 13 warnings. It was working fine yesterday, so I'm not sure what the issue could be. The only change I made was to a line of JavaScript code that i ...

Customized template based on user's dropdown choice

Looking to implement a dynamic template that changes based on user selection. For example, when a dropdown option is chosen, another HTML template should be loaded. I want to approach this in an organized and modular manner, with controllers that can be ...

Exploring file writing using Node Webkit and JavaScript

When developing my app, I encountered an issue with the 'fs' module not functioning as expected. Despite the code being written to write a file, nothing happens when the app is launched. However, if I start the app using the following command: $ ...

Reducing size and Assembling JavaScript and CSS

I find myself in a bit of a dilemma when it comes to using just one JS file and one CSS file for a website. Let me elaborate: As someone who mainly works with CMS platforms like Joomla, I often run into issues during page speed tests where experts recomme ...

Observe the present time in a specific nation

Is there an authorized method to obtain and showcase the current accurate GMT time instead of relying on the easily manipulable local time on a computer? I am looking for a reliable way to acquire the current time in hours/minutes, so I can make calculati ...

Exploring the Uses of SystemJS with TypeScript Loader

Can someone help clarify something about this TypeScript plugin for SystemJS? https://github.com/frankwallis/plugin-typescript/ Here is what the plugin does: This SystemJS plugin allows you to import TypeScript files directly and have them compiled in ...

"Ensuring Contact Information is Unique: A Guide to Checking for Existing Email or Phone Numbers in

I'm encountering an issue with validating email and phone numbers in my MongoDB database. Currently, my code only checks for the presence of the email but does not respond to the phone number. const express = require("express"); const router ...

Establish remote functionality for a JSON AJAX PHP form

Recently, I encountered an issue with my Javascript code that interprets JSON from PHP. Surprisingly, it works perfectly fine on my local server, but when I attempt to transfer the script to a different server, it fails to function. To address this, I have ...

There are no warnings or errors when running Yeoman grunt build, however, the dist folder that is generated is not functioning

When working on an Angular application, I typically run either grunt build or grunt serve:dist to generate my production files and deploy them to a remote server. Despite everything appearing fine with no warning messages in the Yeoman logs and yo doctor g ...

Updating a useState hook in react with a specific condition: a step-by-step guide

Utilizing react hooks (useEffect and useState) in conjunction with firebase has been a seamless process for me. Having a collection of users that can be easily retrieved from firebase, the basic structure of my code appears as follows: const [users, setUs ...

What could be causing the lack of rendering for AngularJS code in Rails, even when AngularJS itself is functioning properly

I recently attempted to incorporate AngularJS into my Rails application. After following the steps below, I confirmed that Angular was running (by typing angular into the dev console), but unfortunately, my angular code in the HAML file was not rendering/ ...

Guide for implementing async/await in conjunction with the eval() function within JavaScript

I'm currently using the eval function to evaluate strings and adding await to it to ensure all values are obtained, but unfortunately the await is not functioning correctly. Here is a snippet of my code: if (matchCard.card.status != "notstarted& ...

To set up Laravel 5 project on Mac OS X, run `npm install` and hang

My Development Environment ⚡️ Node Version: v8.9.4 ⚡️ NPM Version: 5.6.0 ...

There seems to be an issue with the functionality of the Bootstrap Modal

I've been working on incorporating bootstrap login Modal functionality into my code, but for some reason, the screen is not displaying it. Here's what shows up on the screen: https://i.sstatic.net/UIU2w.png The red box in the image indicates whe ...

Clicking on a span will allow you to alter the text within a paragraph located in a

I am hoping to update a paragraph of text with new content when a faux 'link' (span) at the bottom of the page is clicked. This is what I have so far <body> <p> This is the paragraph that should be changed after clicking on the sp ...

Traversing JSON Data using Vanilla JavaScript to dynamically fill a specified amount of articles in an HTML page

Here is the code along with my explanation and questions: I'm utilizing myjson.com to create 12 'results'. These results represent 12 clients, each with different sets of data. For instance, Client 1: First Name - James, Address - 1234 Ma ...