How to trigger an Angular JS route without loading a view

Could someone help me with calling the /auth/logout url to get redirected after a session is deleted?

app.config(['$routeProvider',function($routeProvider) {
    $routeProvider
    .when('/auth/logout',{
        controller:'AuthLogout'
        //templateUrl: not needed

    })
})
.controller('AuthLogout', ['$window','$location', function ($window,$location) {
    $window.localStorage.removeItem('user_username');
    $window.localStorage.removeItem('user_id');
    $window.localStorage.removeItem('user_session_token');
    $location.path('/');
}]);

I'm struggling because I don't actually need a view for the AuthLogout controller. However, if I do not specify the templateUrl in routeProvider, I can't get this to work. But when I do specify a templateUrl, it works.

Does anyone know how I can call the url/controller without having to load a view?

Answer №1

One possible solution is:

.when('/auth/logout', {
    controller: function(){
         //perform necessary actions
    }
})

However, it appears that there may be an issue with your code as the template functions correctly and could potentially be exploited in a similar manner.

For more information, you can visit http://docs.angularjs.org/api/ngRoute/provider/$routeProvider

Answer №2

If you're looking for a solution, consider using a resolve handler as suggested in this post https://github.com/angular/angular.js/issues/1838

Take a look at this handy example where an alert statement is used in the resolve function.

http://jsfiddle.net/Wk7WD/34/

.when('/detail/:id/', {
    resolve: {
        load: function ($route, dataService) {
            alert("hello");
            //You can replace this with your own statements
            return dataService.load($route.current.params.id);
        }
    }
})

Feel free to customize the code inside the resolve function instead of using the alert statement

Answer №3

try utilizing the redirectTo method

app.config(['$routeProvider',function($routeProvider) {
    $routeProvider
    .when('/auth/logout',{
        redirectTo:'/'

    })
});

Fingers crossed that this solution solves your issue :)

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

Updating a column in a SQL Table via an Ajax request

I am attempting to store the on or off value from a form into an SQL database by calling a JS function with an AJAX request that sends it to a PHP page for processing. I seem to be facing some issues with my code and could really use some assistance as the ...

Unable to render HTML through Jquery ajax functionality

success: function(json) { for (var i = 0; i < json.length; i++) { var response = json[i]; $('#rv-container').html('<p>' + response.name + '</p>' + '<span>' + response ...

react scroll event not displaying drop shadow

As a newcomer to JavaScript React, I've been attempting to create a feature where the navbar drops a shadow when the user scrolls down. Unfortunately, it's not working as intended. Can anyone point out what I might have done incorrectly? I suspe ...

The checkbox is currently selected, however, I would like it to be dese

I am facing an issue where I cannot figure out why the password checkbox always turns to checked even when it is supposed to stay unchecked. No matter what, it doesn't behave as I intended and I am struggling to explain this behavior... <template&g ...

What is the best way to include the ID in a Vue.js Ajax request for posts?

Is there a way to pass an "id" obtained as data in vue js? The id is coming as "agnt.basic.actor". Considering the presence of multiple ids, how can I achieve this? <tr v-for="agnt in agentlist"> <td v-if="agnt.basic">{{agnt.basic.actor}}< ...

Unable to retrieve Angular Service variable from Controller

I am facing an issue with my Angular Service. I have set up two controllers and one service. The first controller fetches data through an AJAX call and stores it in the service. Then, the second controller tries to access this data from the service. While ...

"Substituting the $resource master/copy with a new one in the $save callback does not activate

I am currently utilizing angular $resource in conjunction with angular.copy to enable users to reset a form. After the user presses the save button, I invoke $scope.save() which saves the $resource using the $save() method. Inside the callback of the $save ...

res.render() Displaying Data in Frontend using Local Variables

I have a question regarding defining local variables in Express. When I use res.render(view, {variable: variable}), how can these variables be accessed on the frontend? Where are they stored? I attempted to access a variable with console.log(variable), but ...

Buffering ceases on the video

I am experiencing an issue with 4 videos and a preloader, which should hide once all the videos are fully buffered. <div id="preload"> <h1> Download... </h1> </div> <video preload="auto" class= ...

No data found in req.query object in ExpressJS

When I use http.post to send data from Angular to NodeJS, the req.query always comes back empty for me. Here is my server.js setup: const express = require('express'); const cors = require('cors'); const bodyParser = require('body ...

What is the reason behind the failure to update the state via a reducer and Object.assign?

I'm attempting to develop a reducer without utilizing ES6. It's an outmoded PHP application that lacks a build process for transpilation. I am initializing the state: let defaultState = { accountTypes: { individual: { c ...

Utilizing Selenium JavaScript to insert a cookie into a request

Trying to add a cookie to the request in Selenium using JavaScript. I followed the documentation at this link, but my code snippet doesn't seem to pass any cookies to the PHP script below on the server. Here is the client-side JavaScript code: var w ...

Style the code presented within a div tag

I am in the process of creating a JavaScript-powered user interface that can generate code based on user interactions. While I have successfully implemented the code generation functionality and saved the generated code as a string, I am facing difficultie ...

Adjust the size of the Div and its content to fit the dimensions of

Currently, I have a div containing elements that are aligned perfectly. However, I need to scale this div to fit the viewport size. Using CSS scale is not an option as it does not support pixel values. https://i.stack.imgur.com/uThqx.png I am looking to ...

Error: The database query returned duplicate results while using ngRepeat

I have encountered a persistent error that seems to be related to the "fetchCourses()" function in courses.js. Despite various attempts, the error persists and only goes away when I comment out this particular function. My suspicion is on the HTTP request, ...

Obtain one option from the two types included in a TypeScript union type

I am working with a union type that consists of two interfaces, IUserInfosLogin and IUserInfosRegister. The TUserInfos type is defined as the union of these two interfaces. export interface IUserInfosLogin { usernameOrEmail: string; password: string; } ...

AngularJS causing a modal popup to appear even when the associated button is disabled

When using a Bootstrap modal popup form that opens on button click in AngularJS, I noticed that the modal still appears even when the button is disabled. Can someone help me understand why this happens? Here is the code for the button: <a class="btn b ...

I have a JavaScript code that smoothly scrolls to different id's on a webpage, but I need assistance in adding an exception for a particular id

I'm looking to incorporate smooth scrolling for all hash links in my JavaScript. However, I need the id=nav and its resulting hash link, /#nav, to be exempt from this functionality as it interferes with the menu responsiveness. Can someone provide gui ...

Implementing automatic redirection upon clicking using React without the need for manual clicking

I'm experiencing an issue where the page seems to automatically navigate to another page without clicking on the div. Can anyone explain why this is happening? Here's the code snippet for reference: import React, { Component } from "react&q ...

"Encountering an error with ExpressJS where it cannot GET a file, preventing access to other folders' content

My goal is to set up a simple server using expressJS to retrieve data for my Angular application. However, I've encountered an error that says 'Cannot GET/'. This is the code snippet of the webserver I attempted to create: var express = re ...