Setting up AngularJS in a modular way

Lately, I've been consolidating all my functions into a single large JS file. However, I have now decided to divide them into smaller modules to enhance the portability of my application.

My core JS file (main.js) contains the following code:

var App = angular.module("app", ['ui.bootstrap',  'angularMoment', 'chieffancypants.loadingBar', 'ngAnimate', 'ui.sortable', 'ngSanitize'], function ($interpolateProvider, $httpProvider) {
        $interpolateProvider.startSymbol('[[');
        $interpolateProvider.endSymbol(']]');
        $httpProvider.defaults.transformRequest = function (data) {
            if (data === undefined) {
                return data;
            }
            return $.param(data);
        };
        $httpProvider.defaults.headers.post['Content-Type'] = ''
        + 'application/x-www-form-urlencoded; charset=UTF-8';
        $httpProvider.defaults.headers.post['X-Requested-With'] = ''
        + 'XMLHttpRequest';
    });

In another file (e.g., blog.module.js), I have the following:

        (function(window, angular, undefined) {

        'use strict';

        angular.module("app", [])
        .controller('Blog', ['$scope', function ($scope) {
            alert('test');
        }]);
    });

While the main.js file loads along with all its dependencies, the second one doesn't get loaded. The controller seems to be missing. Can anyone provide me with some guidance on what might be causing this issue?

Thank you

Answer №1

It appears that there is a syntax error in the blog.module.js file where you are attempting to create two modules with the same name 'app'. To resolve this issue, update your controller module as shown below:

(function(window, angular, undefined) {

'use strict';

angular.module("app")
.controller('Blog', ['$scope', function ($scope) {
    alert('test');
}]);
});

Answer №2

As mentioned by you:

App = angular.module("app", ['ui.bootstrap', 
...
 angular.module("app", [])

You shouldn't attempt to redefine a module, but rather revisit it

App = angular.module("app", ['ui.bootstrap', 
...
 angular.module("app")

Alternatively, you can establish a new one

App = angular.module("app", ['ui.bootstrap', 'app.controller'
...
angular.module("app.controller")

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

Communicate with every occurrence of Content Script

My chrome extension includes a Content Script and Popup Script. The popup contains a settings page that modifies certain variables on the Content Script by communicating through chrome messaging. The Content Script operates on all YouTube sites, but when ...

Troubleshooting Problem with JQuery in the YouTube Player API

Having some difficulty parsing YouTube video ids to a function that plays them in an embedded player using a combination of JavaScript and jQuery with the YouTube Data and Player APIs. Below is my code: <html> <head> <script src="//aj ...

What is the best way to retrieve the document DOM object within an EJS template?

I am attempting to display a list of participants when the user clicks on the button. However, every time I try, I encounter an error stating "document is not defined". (Please refrain from suggesting the use of jQuery!). <% var btn = document.getEle ...

Displaying data based on specific identifiers in AngularJS

How can I update information based on the unique ID provided in a JSON file. JSON: { "hotels": [ { "id" : 1, "name": "some hotel 1", "category" : [{ "id" : 1, "hotel_id" : 1, "name" : "Cat name 1", "bn ...

Checking for identical inputs in a React component's onChange event: how can it be done?

I'm currently working on implementing an onChange event in React to validate if two input fields are identical, specifically for confirming a password. The goal is to display a message below the input fields as users type, indicating whether the passw ...

Choose the fetched value from the options, or opt for the default value

Despite extensively reviewing the documentation and numerous questions here on SO, I am struggling to find a solution for my current issue. I have a dropdown that is populated with data from a database, and I am using ngOptions with the select as method to ...

Executing a function after the completion of another

Here is my function: function functionName($results) { //do some stuff disableSave() } When this function runs, I want to call the enableSave() function. How can I achieve this? I attempted to pass the function as a callback but I am unsure wher ...

Learn to implement a GET request to an external website and showcase the retrieved data using Express

I'm attempting to send a get request to a basic website and then showcase the value of "phrase" This is my code for index.js router.get('https://corporatebs-generator.sameerkumar.website/', function(req, res, next) { res.render({corp ...

Learn how to retrieve Firebase documents in reverse chronological order using React-firebase-hooks

I'm working on a chat app and I believe that Firebase Cloud Firestore queries documents in ascending order. I have come across solutions online that suggest initializing values as negatives, but since I am dealing with timestamp objects that correspon ...

Is there a way to ensure uniform display of HTML form error messages across various browsers?

Recently, I completed a login form that consists of username and password fields. For the username, I used a text field with the type email, while for the password, I utilized a text field with the type password which requires validation through a regex pa ...

Allow Vue to handle the registration of the datepicker event

Is there a way to notify Vue when the datepicker changes the selected date? new Vue({ el: '#app', data: { termin: '' }, computed: { }, methods: { } }) This is just an input field: <div id="app"> <div c ...

Issue encountered with Framer Motion: Scope is available but no element has been detected

I'm in the process of developing an exciting multiplayer game. The concept is simple: you create a game, share the link or session ID with your opponent, and they join the game. The technology stack I'm using includes React, Next.js, Firebase&ap ...

A guide on converting SVG Files to PDF format with JSPDF using JavaScript

I encountered difficulties converting SVG Files into PDF using JSPDF. Here is the code I tried: var doc = new jsPDF(); var test = $.get('amChart.svg', function(svgText){ // console.log(svgText); var svgAsText = new XMLSerialize ...

Trouble with jQuery: Tackling a Basic String and Variable Issue

I'm having trouble incorporating my variable into this specific string: var liPad = 20; $(this).css({'width' : width , 'height' : height, 'padding' : "'0px' + liPad + 'px'"}); The desired outcome is ...

Divide the table header column "th" into two separate columns

Can someone assist me in achieving the output displayed in the image? I want to separate the header th into two lines like the blue line shown. I need two headers for a single td column. Please help, thank you. https://i.sstatic.net/SwILH.png <style& ...

Issue with data binding click event not functioning on Chrome when used within a nested for loop

Currently, the code below is functioning properly in Internet Explorer. It involves a download link that is disabled based on a condition. However, the same code does not work in Chrome. In Chrome, the Link remains enabled and does not disable as expected ...

Retrieve BD information using AJAX and encountering a null value in the array due to special characters

Hey, I'm using ajax to retrieve an array of data from the database: $.post ( "lib/php/load_food.php", {f:Base64.encode("primeros")}, function(data) { firsts = data; }, "json" ); However, in the 'firsts' array, strings containing accented c ...

Utilizing ExpressJS to save uploaded files using the FileReader object from the front-end

Despite researching multiple posts on this topic, I am still unable to successfully upload a file after numerous adjustments. My React form includes a PDF file upload component which looks like this: <Input onChange={(e) => this.handleFileUpload(e) ...

"Empty $stateParams Issue Encountered in Angular UI-Router While Using My Directive

I have developed a unique directive in my angular application for a custom navbar. The controller of this directive utilizes $stateParams to access a variable named lang, as shown below: .config(function($stateProvider, $urlRouterProvider, LANG) { $u ...

Error thrown by Angular's ngAnimate

I'm running into some issues while attempting to implement animations using ngAnimate. The main problem arises when I try to include the ngAnimate dependency in my app file, which leads to an error being displayed in the console and causing my app to ...