The issue arises when the viewroute.js file is separated from AngularJS, causing it to fail to

Running the code as a single entity works fine, but separating it causes issues with no error output on the console. Even in a separate viewroute.js file, I can debug but not achieve normal behavior. The problem seems to lie in separating services from the main codebase. Any advice or suggestions would be appreciated. I've also experimented with loading jQuery before AngularJS.

The default localhost address is: http://localhost:3000/#!/

The folder structure is as follows: public> 1. controllers> membershipcontroller 2. mgRoute > viewroute.js views - index.html

The index.html file contains the following script loading order:

<!DOCTYPE html>
<html ng-app="tdmModule">

<head>
    <title>welcome</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-resource.js"></script>
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="ngRoute/viewroute.js" type="text/javascript"></script>
    <script src="controllers/app.js" type="text/javascript"></script>
    <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="stylesheets/style.css" rel="stylesheet">

</head>

<body>

    <div>
        <div class="container pt-5">
            <div ng-view></div>
        </div>
    </div>

</body>

</html>

Here's the content of my app.js file:

var app = angular.module("tdmModule", ["ngRoute"]);

app.config(['$qProvider', function($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}]);

//This section needs to be separated into its own file
app.config(['$routeProvider', '$locationProvider',
    function($routeProvider, $locationProvider) {
        $routeProvider
            .when("/", {
                templateUrl: "home",
                controller: 'homeController'
            })
            .when("/membershipdetails", {
                templateUrl: "membershipdetails",
                controller: 'membershipController'
            })
            .when("/help", {
                templateUrl: "help",
                controller: 'helpController'
            }).otherwise({
                redirectTo: '/'
            });
    }
]);
// End separation 

app.controller('membershipController', function($scope, $filter, $http,
    $httpParamSerializer, $location, membershipService, setnotanoption,
    compileservice) {
    // Controller logic here...
});

I attempted to isolate the following code into the viewroute.js file: I can confirm that the debugger hits, indicating it loads correctly. However, when commented out from the app.js file, the page fails to load without any errors appearing in the console.

var app = angular.module("tdmModule", ["ngRoute"]);

//Error handling configuration
app.config(['$qProvider', function($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}]);

debugger; ///this debugger is being hit
app.config(function($routeProvider) {
    $routeProvider
        .when("/", {
            templateUrl: "home",
            controller: 'homeController'
        })
        .when("/membershipdetails", {
            templateUrl: "membershipdetails",
            controller: 'membershipController'
        })
        .when("/help", {
            templateUrl: "help",
            controller: 'helpController'
        }).otherwise({
            redirectTo: '/'
        });
});

Answer №1

It appears that the tdmModule is being declared twice.

You have two options to address this issue:

  1. Include your routes as part of the tdmModule

    If you choose this path, your viewroutes.js will appear as follows:

    var app = angular.module("tdmModule");
    
    app.config(function($routeProvider) {
      $routeProvider
        .when("/", {
            templateUrl: "home",
            controller: 'homeController'
        })
        .when("/membershipdetails", {
            templateUrl: "membershipdetails",
            controller: 'membershipController'
        })
        .when("/help", {
            templateUrl: "help",
            controller: 'helpController'
        })
        .otherwise({
            redirectTo: '/'
        });
    });
    
  2. Declare your routes as part of a new module, named routesModule, and make it a dependency of your tdmModule.

    If you go with this option, your viewroutes.js will look like this:

    var app = angular.module("routesModule", ["ngRoute"]);
    
    app.config(function($routeProvider) {
      $routeProvider
        .when("/", {
            templateUrl: "home",
            controller: 'homeController'
        })
        .when("/membershipdetails", {
            templateUrl: "membershipdetails",
            controller: 'membershipController'
        })
        .when("/help", {
            templateUrl: "help",
            controller: 'helpController'
        })
        .otherwise({
            redirectTo: '/'
        });
    });
    

    Additionally, your app.js should be updated as well:

    var app = angular.module("tdmModule", ["routesModule"]);
    ...
    

I hope this information proves helpful.


Edited

The first option may not work due to the order in which your scripts are loaded.

It's crucial to declare the tdmModule before defining a config on it for it to function properly.

Therefore, ensure that you import controller/app.js before viewroutes.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

Utilize axios to retrieve data and pass it as props to a component

I am new to react and struggling with a basic issue. I need help passing an array of values from an external service as a prop to another component. Approach render() { let todolist="default"; axios.get('http://localhost:8888/todoitems').t ...

When the button is clicked, it triggers a change and updates the records in the database. The

There is a single button named "delete" that, when clicked by a user, will change the text to "restore" and perform a delete action in the database. The same functionality should apply to the restore button, where clicking it will change the text back to " ...

Mastering Vue 3: Simplifying a reactive array of objects while maintaining reactivity

Struggling with maintaining reactivity in Vue 3 when flattening a nested array of objects. Unfortunately, my attempts result in crashing and browser hang-ups. In my Vue 3 component, I have an array structured as a list of objects: this.grouped = [ ...

Obtaining a value from HTML and passing it to another component in Angular

I am facing an issue where I am trying to send a value from a web service to another component. The problem is that the value appears empty in the other component, even though I can see that the value is present when I use console.log() in the current comp ...

Stopping Popups in Chrome When Printing with JavaScript

Javascript function printPage(htmlPageContent) { var newWindow = window.open("about:blank"); newWindow.document.write(htmlPageContent); newWindow.print(); } In this code snippet, we are creating a new window 'newWindow' and then using ...

When a newer request is made, I would like to cancel my previous one

Currently, I am working with a text box that triggers an HTTP call for each input character. However, my challenge lies in the fact that I need to be able to cancel the previous request if the user enters the next character before receiving a response. ...

Retrieving all users in Sqlite database with a specific value

I am looking to identify and access each user who has a specific value in their row. Here is an example code snippet of what I want: sql.prepare("SELECT * FROM raid WHERE raid1 > 0 AND NOT id='685337576810610734'").get().forEach(async (user) ...

Exploring techniques for creating realistic dimensions in CSS

My goal is to create a responsive website that accurately displays an object with specified dimensions, such as a width of 100mm, regardless of the user's screen resolution. However, I am facing challenges in achieving this consistency across all devi ...

Guide to integrating a fruit product checklist within a React application

Seeking assistance in incorporating a checklist into my react application, to only be visible after a specific button is clicked. Upon reviewing productFruit's documentation, it appears that I need to utilize the following code snippet: useEffect(() ...

Tips for dynamically utilizing dispatch in React

I am facing an issue with the code below where the dispatch method seems to be fetching the previous userId parameter. The sequence of actions is as follows: I first navigate to the users-list, then proceed to the user-info (displays correctly), but upon ...

Unusual Angular.js Quirk - Dropdown menu requires double clicks on button to open

When using angular.js, I encountered an issue with a button that is supposed to display a drop-down menu. The problem is that I have to click on the button twice in order for the drop-down menu to actually appear. The first click registers as the button li ...

Stop unauthorized access to the controller in ui-router

Below is a snippet from my $stateProvider configuration .state('main', { url: '/', auth: true, views: { '@': { templateUrl: 'main.html', controller: 'MainController' ...

Using the window.history.pushState function triggers a page reload every time it is activated

I've been attempting to navigate from page to page without the need for a reload. I came across suggestions that using window.history.pushState() could achieve this, however, it appears that the page is still reloading. Furthermore, an attempt ...

Magnific Popup is causing a glitch in my Ajax cart slider, preventing my code from functioning properly

Currently, I have implemented an Ajax cart slider that slides from right to left whenever an item is added to the cart. Customers are given the option to add a product with an image to their cart and can view the image directly from the cart by clicking on ...

Guide on building a command-line application that generates a directory along with its contents when executed

Whenever I have a reporting task to complete, the same routine always follows: Create a new folder Copy the necessary HTML/JS files into it Edit the files as needed for the task Upload them to either S3 or a local directory I am interested in automating ...

Using a for loop to add a nested div inside another div

Hey there, I've got a question about the loop that's currently in my code. Right now, I'm doing it manually and it seems to be working that way. $( '<div id="demo1" > <span class="value"></span> </div> ...

Is there a way to verify that the header includes the specific keywords I need?

<h1 class="page-header">Teams<span class="badge team-cnt-badge" data-toggle="tooltip" data-original-title="Total {{teamCnt}} Teams">{{teamCnt}}</span></h1> Upon viewing the header on the page, you will notice it displays a text str ...

The post method is functioning properly in browsers such as Firefox, Internet Explorer, and Chrome; however, it is not working in the Edge browser

I am encountering an issue with a post method in the Edge browser. Even though I am able to receive responses for the same request in other browsers like Internet Explorer, Chrome, and Firefox, Edge seems to be not responding at all. Despite conducting a s ...

Consecutive pair of XMLHttpRequest requests

Is it possible to create a functionality where the page index.php sends a Javascript variable called idToken, which is then received in another page called token.php using Javascript as well? In the token.php page, there will be more code that processes ...

Tips for patiently waiting for an axios request to finish

Within my vuejs application, I am faced with the challenge of initializing an object using data retrieved from an ajax call: let settings = {} api.readConfig().then((config) => { settings = { userStore: new Oidc.WebStorageStateStore(), author ...