Encountering a problem in Angular after combining multiple AngularJS files into one single JS file using Gulp

When using gulp-concat to combine all angular js files into one, I encountered an error in the Chrome console when running the application:

angular.js:13708Error: [ng:areq] http://errors.angularjs.org/1.5.7/ng/areq?p0=userboxController&p1=not%20a%20function%2C%20got%20undefined

This is my gulp task:

gulp.task('scripts', function () {
  return gulp.src(['js/app/*.js', 'components/*/*/*.js'])  
    .pipe(concat('appscript.js'))
    .pipe(minify()) 
    .pipe(gulp.dest('./dist/js/'));
});

The gulp-concat tool combines specific angular js files into appscript.js like so:

angular.module('app',[]);     


angular
    .module("app", [])
    .controller("paymentCtrl", ['$scope', '$http', function ($scope, $http) {
        $http.get('data/payments.json').then(function (payments) {
            $scope.payments = payments.data;
        });

        $scope.saveEntity = function () {
            console.info("goog");
        }
    }]); 

angular
    .module("app",[])
    .controller("userboxController", ['$scope', '$http',function ($scope, $http, usersService) {
        usersService.getCurrentUser().then(function (user) {
            $scope.user = user.data;
        });
    }]);


angular
    .module("app",[])
    .controller("usersController",['$scope', '$http','usersService', function ($scope, $http, usersService) {
        usersService.getAll().then(function (users) {
            $scope.users = users.data;
        });
    }]); 


angular
    .module("app", [])
    .directive('usersGrid', function () {
        return {
             templateUrl : 'components/users/template/grid.html'
        }
    }); 

But why is Angular throwing errors?!

Answer №1

This issue is not related to merging.

It seems like you are creating app modules repeatedly with:

angular.module('app', [])

To avoid this, initialize the module in one place only so that you can use the same module every time without needing to specify [] brackets again.

You can check out a working example on Plunker here

var myApp = angular.module('app');

myApp.controller("paymentCtrl", ['$scope', '$http', function($scope, $http) {
  $http.get('data/payments.json').then(function(payments) {
    $scope.payments = payments.data;
  });

  $scope.saveEntity = function() {
    console.info("Information saved successfully");
  }
}]);

myApp.controller("userboxController", ['$scope', '$http',       function($scope, $http, usersService) {
  $scope.user = 'abc';

}]);


myApp.controller("usersController", ['$scope', '$http', 'usersService', function($scope, $http, usersService) {
  usersService.getAll().then(function(users) {
    $scope.users = users.data;
  });
}]);


myApp.directive('usersGrid', function() {
  return {
    templateUrl: 'components/users/template/grid.html'
  }
});

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

Is it possible to invoke a code behind method from the window's onbeforeunload event

Is there a way to call a code behind method (such as saving data to a database) from window.onbeforeunload? I am unable to use PageMethods due to non-shared members like textboxes and comboboxes being present in the save method. How can I manage this scena ...

Changing in height by utilizing javascript's style.height animation

When attempting to adjust the height property using JavaScript instead of jQuery, a challenge arises. The issue lies in the inability to reset the height back to zero after setting it to the scrollHeight of the element. The following is the JavaScript cod ...

Error: Unable to locate module: Unable to resolve '@/src/components/layout/Header'

Compilation Error Encountered issue in Next.js (version 14.2.4) while trying to locate '@/src/components/layout/Header' at ./src/app/layout.js:3:1 Suddenly, 2 days ago the code was functioning properly but now it's throwing this error. Er ...

Enhancing Security Through Authentication and Loopback API Integration

I have created a loopback api which can be accessed at 'http://localhost:8100/api/insertsubmitorder/insertOrders'. However, I am concerned about the security of this API. Is it possible to secure and authenticate it using OAuth2 or any other meth ...

Dynamic rule addition with JQuery Validation

I am searching for a method to dynamically incorporate the jQuery validation rules, as they are needed in various locations. For instance, a user can edit news in two different ways. The first method involves using a standard form with jQuery validation, ...

Using Angular ng-model with Bootstrap Modal is problematic when attempting to use it multiple times

Imagine you have a table with 10 lists of operators, each with an edit button. When you click on the edit button for operator 1, the modal pops up and displays the operator data in the input field as expected. However, when you close the modal and try to e ...

Invite your friends to join my innovative categorization platform with a user-friendly layout similar

I am currently developing a categorization service and I would like it to function similar to Facebook's invite/tagging feature. Has anyone here had experience implementing this type of functionality before? This includes: Auto-completion based on ...

Using JavaScript, align an image's coordinates with a specific coordinate within its parent container

I am looking to change the position of my relative image dynamically so that it aligns with a fixed pointer's coordinate that is absolute to it ...

Sorting through an array of objects using criteria from a separate array of objects

Currently, I am faced with the task of filtering an array of objects based on criteria from another array. Essentially, I am looking to refine a list based on filters selected by the user. To illustrate, let's consider my initial list of results: [ ...

send array to the sort function

How can I sort a data array that is returned from a function, rather than using a predefined const like in the example below: const DEFAULT_COMPETITORS = [ 'Seamless/Grubhub', 'test']; DEFAULT_COMPETITORS.sort(function (a, b) { re ...

What is the process for reaching individuals within a nested object?

I have been struggling to access the elements of a nested object without any success. Despite looking through similar questions on stackexchange (such as this), I have not been able to resolve my issue. I attempted to access the final element using console ...

Using JavaScript to override a specifically declared CSS style

In the process of working with a database, I come across HTML that includes "spans" colored in different shades. An example would be: <div id="RelevantDiv">the industry's standard <span style="background-color: red">dummy text ever since ...

Combining two or more arrays containing similar values into a single array

Within my array, there are 100 subarrays, each containing 3160 values of an object with two attributes: a sequence number (which only appears if it is present as 1), and another value of either 0 or 1. My goal is to merge all 100 arrays into one single ar ...

Modify the div's visibility based on selected option in the dropdown menu

In my PHP file, I have the following codes: <section id="placeOrder"> <h2>Place order</h2> Your details Customer Type: <select name="customerType"> <option value="">Customer Type?</option> <option value="ret ...

Select any menu option to return to the one-page layout and then scroll down to find the location

I'm wondering if there is a way to navigate back from an external page to a specific section on my one-page website with a fixed menu? On my one-pager website, I have a fixed menu that includes a "apply for a job" button. When clicked, it takes users ...

implementing lazy loading in AngularJS uib-tab

I am facing an issue with a uib-tab that has a heading and an ng-click method that toggles a boolean variable to load the content. The code is as follows: <uib-tab class="height-full"> <uib-tab-heading ng-click="vm.loadTab(2)"> ...

What could be the reason why the CSS class is not being applied?

Can you explain why my CSS class isn't being applied? I included my external CSS file like this: @import url("https://gist.githubusercontent.com/eirikbakke/1059266/raw/d81dba46c76169c2b253de0baed790677883c221/gistfile1.css"); And used it in my code ...

JQuery problem with toggling the menu icon

I have a toggle control that switches between a menu icon and an X when clicked, displaying an overlay nav as well. The issue is: Clicking on any link in the overlay nav does not switch/toggle the X back to the default menu icon. In the code below, I inc ...

Headers permitted for CORS blocking

Greetings, I have recently developed an API and frontend using React, utilizing axios for making requests. I implemented an authorization header with axios and authenticated it on the PHP server-side. However, there seems to be an issue where the Authoriza ...

Control the button's activation status by accepting or unaccepting the two checkbox conditions

In my search through stackoverflow for help on enabling/disabling a button conditionally, I found some assistance but nothing quite matched what I needed. My situation involves two checkbox conditions rather than just one. The button should only be enable ...