$routeProvider is erroring out with the message "Uncaught Error: [ng:areq] Argument 'fn' is not a function, received a string instead."

Currently, I am in the process of creating routing logic using ngRoute within Angular JS. Below is the snippet of code I am working with.

index.js

(function() {
  'use strict';

  function configureRoutes($routeProvider, $httpProvider, cfpLoadingBarProvider, $tooltipProvider) {
    $routeProvider.otherwise({redirectTo: '/404'});
    $httpProvider.defaults.withCredentials = false;
    $httpProvider.defaults.headers.common['content-type'] = "application/json";
  }

  angular
    .module('pacman', ['ngCookies', 'ngRoute', 'ui.bootstrap', 'ui.validate',
                      'angular-cache', 'angular-loading-bar', 'angular-md5', 'rt.iso8601', 'ngAnimate']
            )
    .config(['$routeProvider', '$httpProvider', 'cfpLoadingBarProvider', '$tooltipProvider', configureRoutes])
    .run(['$rootScope', '$location', '$modalStack', '$cookies']);
})();

app.controller.js

(function() {
  'use strict';

  function configureRoutes($routeProvider) {
    $routeProvider.when('/', {
      templateUrl: 'app/components/landingpage/landingpage.html',
      controller: 'appController'
    });
  }

  function appController($scope, $rootScope, $location) {
    $scope.submitLogin = function() {
      alert("Successfully loggedIn");
    };
  }

  angular
    .module('pacman')
    .controller('appController', ['$scope', '$rootScope', '$location', appController])
    .config(['$routeProvider', configureRoutes]);
})();

notFound.controller.js

(function() {
  'use strict';

  function configureRoutes($routeProvider) {
    $routeProvider.when('/404', {
      templateUrl: 'app/components/notFound/404page.html',
      controller: 'notFoundController'
    });
  }
  function notFoundController($scope, $rootScope, $location) {
    debugger;
  }
  angular
    .module('pacman')
    .controller('notFoundController', ['$scope', '$rootScope', '$location', notFoundController])
    .config(['$routeProvider', configureRoutes]);
})();

This application involves loading different controllers depending on routes specified. However, upon launching the app, an error arises in the final controller's '$routeProvider' section.

Error Message: Uncaught Error: [ng:areq] Argument 'fn' is not a function, received string http://errors.angularjs.org/1.4.8/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20string

I'm currently stuck and unable to identify the root cause of this issue. Any help or pointers would be greatly appreciated. Below is the order of my library bundle for reference.

'node_modules/jquery/dist/jquery.js',
'node_modules/angular/angular.js',
'node_modules/angular-route/angular-route.js',
'node_modules/jquery.transit/jquery.transit.js',
'node_modules/angular-cache/dist/angular-cache.js',
'node_modules/angular-cookies/angular-cookies.js',
'node_modules/angular-loading-bar/build/loading-bar.js',
'node_modules/angular-ui-validate/dist/validate.js',
'node_modules/chart.js/Chart.js',
'node_modules/angular-md5/angular-md5.js',
'node_modules/angular-iso8601/dist/angular-iso8601.js',
'node_modules/angular-animate/angular-animate.js',
'node_modules/angular-chart.js/dist/angular-chart.js',
'node_modules/rx/dist/rx.all.js',
'node_modules/angular-ui-bootstrap/ui-bootstrap-tpls.js',
'node_modules/bootstrap/dist/js/bootstrap.js'

Your assistance is much appreciated.

Answer №1

The issue lies within your index.js file where you are defining the run method on your Angular app.

angular
    .module('pacman', []) // removed dependencies for brevity
    .run(['$rootScope', '$location', '$modalStack', '$cookies']);

The final argument in the array provided to run should be a function, however, you have made an oversight and forgot to include a function. Adjust your run by adding some functionality as shown below or remove it if it serves no purpose:

angular.module('pacman', []) // removed dependencies for brevity

       .run(['$rootScope', '$location', '$modalStack', '$cookies',

                function($rootScope,$location,$modalStack,$cookies){
                    // add your code here
            }]);

Answer №2

The order in which you declare your Angular JS file and jQuery in the index.html is crucial.

Ensure that 'node_modules/angular/angular.js' comes before 'node_modules/jquery/dist/jquery.js'.

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

Provide the output once the image has been read by the reader and converted into base 64

After clicking the button, I am trying to convert the image to base64 and return the result, but it is coming back blank. Why could this be happening? $("#addNewProduct").click(function(){ var uploadImg1 = convertToBase64(document.getElementById("up ...

Incorporate React Components into a traditional HTML, CSS, and JavaScript project

Can I incorporate a React Native Component (https://www.npmjs.com/package/react-native-gifted-chat) into my vanilla JavaScript project? Is it feasible to embed a React Native component within a vanilla JavaScript project? If yes, then what is the process ...

Executing the removal by _id command in mongodb does not function as intended

Initially, I thought implementing a delete function for my mongodb in the MEAN stack would be straightforward. However, I've encountered issues and haven't found a solution on Google or Stack Overflow. In my application, I am working with Users ...

Is it possible to execute a PHP command using JavaScript?

Successfully invoking PHP from JavaScript using AJAX: function fun() { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { document.getElementById("content").innerHTML = xhr.responseText ...

Can the ternary operator be utilized in CSS styling?

The situation is as follows: $scope.toggleSlider = function() { $('.sliderButton').css('left','0') ? $('.sliderButton').css('left', '173px') : $('.sliderButton').css('left ...

Enhance npm package by implementing custom functionality with React Component

I have designed an app that bears a striking resemblance to the following code: class MyCustomApp extends React.Component { constructor (props) { super(props) this.state = { tags: [ { id: 1, name: "Oranges" }, { id: 2, ...

Testing the disappearance of text in a textarea using Cypress

Is there a way to test the textarea text that disappears once you start typing? Even after I type something, no changes are reflected in the displayed document. https://i.sstatic.net/rNjil.png I am trying to verify that the texts such as Enter a comment ...

Is it possible to perform a search query on the iTunes search API using JavaScript?

I have been attempting to fetch information from the App Store regarding a specific app, but I continue to encounter the error message below. XMLHttpRequest cannot load https://itunes.apple.com/lookup?id=<some-app-id>. No 'Access-Control-Allow ...

Securely transfer item to customer in Node.js/express

One commonly asked question revolves around transmitting an object from Node.js/Express.js to the browser securely. While using JSON stringify is a common method, it can pose risks if the object contains user-input data, leaving the system vulnerable to sc ...

Using Javascript to Divide Table into Separate Tables each Containing a Single Row

Is there a way to divide the content of this table into three separate tables using JavaScript? It is important that each table retains its header information. Unfortunately, I am unable to modify the id's or classes as they are automatically generat ...

Implementing a function to detect clicks outside of a Bootstrap modal in Angular

As stated in the Bootstrap Modal documentation, handling clicks outside of a modal can be done with hide.bs.modal. Many examples online demonstrate this using jQuery. I am curious if anyone can assist me in implementing this functionality in Angular? I am ...

Wrapping Divs encircling a Round Div

Looking for advice on how to place 5 div elements around a circular div. Here's the code I currently have: .main{ border: 2px dotted #000000; border-radius: 50%; width: 500px; height: 500px; } .cirlce1{ height: 50px; width: 50px; border: 2px dotte ...

Replace the current picture with a newly uploaded one and keep it consistent

On my webpage, there is an image that I want to be able to replace by clicking on it and selecting a new image from the file uploader without showing the upload button. Once the new image is uploaded, I'd like it to replace the current image, and for ...

The submit button remains disabled even after verifying all the required fields

I have a registration form with basic customer details and a Submit button. I have successfully validated all the fields using Ajax, except for the Confirm password field which is being validated using JavaScript. The issue I am facing is that when I val ...

Tips for changing date format within Ajax javascript code

I am working with a JavaScript AJAX code: success:function(res){ var _html=''; var json_data=$.parseJSON(res.posts); $.each(json_data,function (index,data) { _html+='<span class=&apo ...

What is the best way to implement media queries for mobile phones and desktop computers?

I've come across similar questions before but still can't wrap my head around it. Here's my dilemma: I want the index page of my website to display in desktop layout on desktops and mobile jquery on mobile devices. Currently, I have set up m ...

Unexpected behavior encountered when running Angular 8 radio button checked function

I have an Angular 8 web app with some unique logic implemented as shown below: HTML: <div *ngFor="let item of selectedItems;"> <input type="radio" [(ngModel)]="mySelectedItem" [value]="item.key" (ngModelChange)="setCh ...

When you click on the span element, why doesn't its text automatically populate the input field? This issue may involve JavaScript and jQuery

Why doesn't the text get transferred to the input when the span is clicked? HTML <script> $(document).ready(function () { $('#ajax-service').keyup(function () { $.ajax({ data: $ ...

Omitting specific modules from the Webpack DLL compilation

I have a universal JavaScript application built with Webpack. I've utilized the DLL plugin to pre-build all my node_modules, but recently encountered an issue when adding a new library. This specific library is causing the DLL build process to fail (d ...

Modifying table background color using AJAX and jQuery?

Scenario: My web page is designed to automatically search for a specific cell input by the user. If the cell has been input with a value, the table's background color will turn red; otherwise, it will remain green. Currently, the table has not been p ...