"Dealing with an array of routes in AngularJS $

Clicking on the Home link triggers the goTo function, but it redirects to a blank page as shown:

https://i.sstatic.net/qerBI.png

Upon clicking on the browser's back button, it redirects to the desired page (firstPage).

https://i.sstatic.net/oaANp.png

Any insights on what might be causing this issue?

HTML:

<footer ng-controller = "footerLink">
        <a href = "#"  ng-click="goTo('/firstPage')"> Home </a>
        <a href = "#"> About us </a>
        <a href = "#"> Our Team </a>
        <a href = "#"> Blog </a>
        <a href = "#"> Services </a>
        <a href = "#"> Portfolio </a>
        <a href = "#"> Contact </a>
        <a href = "#"> Sitemap </a>
    </footer>

JS

angular.module('myApp.view1', ['ngRoute'])
    .config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
        $routeProvider.
            when('/view1', {
                templateUrl: 'view1/view1.html',
                controller: 'View1Ctrl'
        }).
            when('/buttonView', {
                templateUrl: 'buttonView/buttonView.html',
                controller: 'buttonViewCtrl'
        }).
            when('/firstPage', {
                templateUrl: 'view2/view2.html',
                controller: 'footerLink'
        }).
            when('/home', {
                templateUrl: 'view1/view1.html',
                controller: 'logo'
        });
    }])

.controller('footerLink',  ['$scope', '$location', function($scope, $location) {
    $scope.goTo = function (url) {
      $location.path(url);
     };
}])

UPDATE

Instead of calling another function, I directly redirected the anchor to the firstPage route. Here is the updated code:

<footer ng-controller = "footerLink">
        <a href = "#/firstPage"> Home </a>
        <a href = "#"> About us </a>
        <a href = "#"> Our Team </a>
        <a href = "#"> Blog </a>
        <a href = "#"> Services </a>
        <a href = "#"> Portfolio </a>
        <a href = "#"> Contact </a>
        <a href = "#"> Sitemap </a>
    </footer> 

Answer №1

The reason for this issue is the absence of a redirection rule or route configuration for '/'. You should consider redirecting the user to the home page if there is no matching route.

angular.module('myApp.view1', ['ngRoute'])
    .config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
        $routeProvider.
            when('/view1', {
                templateUrl: 'view1/view1.html',
                controller: 'View1Ctrl'
        }).
            when('/buttonView', {
                templateUrl: 'buttonView/buttonView.html',
                controller: 'buttonViewCtrl'
        }).
            when('/firstPage', {
                templateUrl: 'view2/view2.html',
                controller: 'footerLink'
        }).
            when('/home', {
                templateUrl: 'view1/view1.html',
                controller: 'logo'
        }).otherwise({redirectTo: '/home'});
    }])

.controller('footerLink',  ['$scope', '$location', function($scope, $location) {
    $scope.goTo = function (url) {
      $location.path(url);
     }; // this function is not really required
}]);

Rather than creating an additional function for redirecting users, consider using the 'href' attribute directly in the HTML, as no other processing is done there besides redirecting the user.

<footer ng-controller = "footerLink">
    <a href = "/firstPage"> Home </a>
    <a href = "#"> About us </a>
    <a href = "#"> Our Team </a>
    <a href = "#"> Blog </a>
    <a href = "#"> Services </a>
    <a href = "#"> Portfolio </a>
    <a href = "#"> Contact </a>
    <a href = "#"> Sitemap </a>
</footer>

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

Leveraging JavaScript for pricing calculations within asp.net framework

I am currently working with a textbox in a gridview and trying to calculate values using JavaScript. My code seems to be error-free. The goal is to multiply the quantity by the rate to get the total price. function totalise(price, rate, qt) { var qt ...

When making a POST request with axios, the req.body object appears to be empty. However, when using the 'request' module, the body is populated as expected

Providing some context - My NodeJS server is running on port 3001 and my React application on port 3000. I've configured a proxy in my React application's package.json to redirect all requests to port 3001 - "proxy": "http://localhost:3001" H ...

Having issues with Protractor unable to locate CKEditor during testing proceedings

When working with Protractor on a non-Angular page, I am trying to locate the CKEditor instance in order to update the data. The following code snippet works in the Chrome console: CKEDITOR.instances.html_editor.setData("Hello") However, when I attempt t ...

Incorporate HTML elements into a React component

Is there a way to render (move) a DOM element into a React Component? I am facing a situation where I have integrated a third-party script into my React project (such as incorporating a live chat widget with a script). This script generates a DOM node when ...

Issue encountered while attempting to load a JSON file into an ExtJS JSONPStore due to a

After validating a json file with json.bloople.net, the file begins as follows: { "sepString": "--", When attempting to load this json file into an ExtJS JsonPStore, Chrome displays the error: Uncaught SyntaxError: Unexpected token : in line 2 What ...

What is the best way to populate empty dates within an array of objects using TypeScript or JavaScript?

I am trying to populate this object with dates from today until the next 7 days. Below is my initial object: let obj = { "sessions": [{ "date": "15-05-2021" }, { "date": "16-05-2021" }, { "date": "18-05-2021" }] } The desired ...

Only allowing designated email addresses to authenticate using Gmail Oauth

Is there a way I can limit logins by doing the following? I'm facing an issue where the page keeps reloading constantly after logging in once. function onSignIn(googleUser) { var profile = googleUser.getBasicProfile(); console.log('Name: ...

The table headers in the second table do not match the queryAllSelector

I've encountered an issue with my JavaScript snippet that displays a responsive table. When I use the snippet for a second table with the same class, the formatting gets messed up on mobile devices (try resizing your screen to see). It seems like the ...

Using Angular's ng-include directive within a Jade template

I am attempting to insert a header using ng-include from Angular into my Jade template: doctype html html(lang="en") head meta(charset='UTF-8') meta(name='fragment', content='!') base(href='/& ...

Error: Value not defined in the (Node, Express, Pug, JQuery) environment

I'm encountering a common issue as a beginner and could really use some assistance. I have tried multiple solutions but still can't resolve the error "ReferenceError: $ is not defined" when attempting to use jQuery. My project structure looks lik ...

The Blender model imported into Three.js appears to have been rendered in a lower resolution than expected

I recently brought a gltf model into my Three.js project, which was exported from Blender. The model appears perfectly rendered in , but in my Three.js project, it seems to have lower quality as depicted in these screenshots: https://ibb.co/qrqX8dF (donm ...

Why won't my picture track the movement of the cursor?

I am trying to make my image follow the cursor using a specific code, but it doesn't seem to be working. I'm having trouble figuring out what's wrong with it. Here is the code snippet that I'm currently using: function followIt(e) ...

Is it necessary to configure modules using app.use in every route in express.js?

I recently learned that it is best practice to include the express module individually in each route file, rather than globally in app.js. Now I'm questioning whether I need to duplicate all the app.use statements in each route file or if I can just ...

Modify the key within an array of objects that share a common key

I have an object structured as follows: NewObjName: Object { OLDCOLUMNNAME1: "NEWCOLUMN_NAME1", OLDCOLUMNNAME2: "NEWCOLUMN_NAME2", OLDCOLUMNNAME3: "NEWCOLUMN_NAME3"} Next, there is an array containing objects in this format: ...

The variable remains undefined, despite the fact that the code executes correctly in a different location

I'm currently working on a multiplayer game in three js and integrating socket.io for real-time communication. I have all the player characters stored in an array called players on the server side. When each client connects, I send them the list of p ...

Is there a way to automatically redirect the server URL when a file is modified?

I am currently experimenting with a function that is supposed to only display a message in the console without redirecting the actual URL of my server when a file is changed. watcher.add("/home/diegonode/Desktop/ExpressCart-master/routes/2.mk"); watche ...

Unable to append item to document object model

Within my component, I have a snippet of code: isLoaded($event) { console.log($event); this.visible = $event; console.log(this.visible); this.onClick(); } onClick() { this.listImage = this.imageService.getImage(); let span = docu ...

Having trouble with routing in Angular

Attempting to create a REST API for the TodoApp built in Angular. I have successfully set routes for ADD, UPDATE, and GET ALL, but am encountering issues with the DELETE method. Here is my angular controller: angular.module('todoListApp') .contr ...

localization within vue component

if (self.form.pickupTime == '') { self.formError.pickupTime = 'Please enter a pickup time that is ready for collection.'; status = false; return status; } else { self.formError.pickupTime = ''; } I am struggling ...

When is the best time to assign properties to mobx domain objects?

When it comes to tackling a specific problem in my react/mobx application, I'm facing challenges finding an optimal solution. Imagine having multiple boxes that need to be positioned on the screen based on various factors such as interdependency betwe ...