Revisit the promise in AngularJS after making adjustments to the page and reload

1) The login page redirects to the home page after a successful login.

if (data.status == CONSTANTS.RETURN_SUCCESS) {
      $location.path("/home");
      $route.reload();
}

2) I have implemented a promise to fetch data from the database before loading the home page controller in my app.js code:

.when('/home',
    {
        templateUrl: '/views/home.html',
        resolve: {
            'myAssetsData': function (myAssetsDataPromise) {
                return myAssetsDataPromise.promise;
            }
        }

    })

The promise calls a function "setAssetData()" to retrieve and set service level variables.

.service('myAssetsDataPromise', ['myassets_service', '$log', function (myassets_service, $log) {
    $log.debug("Initialize myAssetsDataPromise");
    var return_data = {}
    //fetch and set some service level variables
    var promise = myassets_service.setAssetData()
    return {
        promise: promise,
        getAssets: function () {
            return return_data.status = "success";
        }
    };

}

3) The home page contains a logout link that clears the session on the server side and redirects back to the login page.

$location.path("/");

4) Everything functions correctly on the initial visit. However, upon logging out and then logging back in or revisiting the home page, the promise does not execute again unless the page is reloaded or history/cache is cleared. Even using $route.reload() does not solve the issue.

Although it's understood that once a promise is resolved, it won't reload, the goal is to trigger the promise again when returning to the page.

Answer №1

Everything seems to be in order. It appears that the issue lies in assigning the result to a variable

var promise = myassets_service.setAssetData()

Try updating your code.

.service('myAssetsDataPromise', function (myassets_service, $log) {
    return {
        promise: myassets_service.setAssetData()
    };
})


.service('myassets_service', function () {
    return {
        setAssetData : function ($q) {
            var data = {'your_data':'what_ever'};
            console.log('setAssetData called', data);

            var deferred = $q.defer();
            deferred.resolve(data);
            return deferred.promise;
        }
    }
})

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

Understanding how to utilize variables from Angular within jQuery

I have a variable called room_price and I am looking to utilize it in jQuery. Is there a way to achieve this? $http({ method:'GET', url: '' }).then(function(response){ var roomdata = respons ...

Strategies for removing duplicate vertices in geometry through Three.js updates

Currently, I am facing an issue with changing vertex positions in my mesh using three js. It appears that three js stores duplicates for each vertex in the mesh. In my specific scenario, I have 4 duplicates of a vertex, and when I modify one using mesh.g ...

Utilize a promise or await statement with a dynamically generated string variable

Currently, I am constructing a mongoose query and saving it in a variable named query. Below is the snippet of code showcasing this: let query = "Product.find(match)"; if (requestObject.query.sortBy) { query = query.concat(".", &quo ...

Tap on the option labeled 'Google' within the Google Maps application

I recently added Google Maps to my AngularJS mobile project, but I am encountering an issue where a "Google" tag appears in the bottom left corner of my map. When clicked, it redirects me to the Google Maps app and prevents me from returning to my own ap ...

How can I use Jquery to loop through each combobox on the page and delete those with a specific class name?

I am currently dealing with a page that contains multiple combo-boxes. Each combo-box has a default option set as empty, which is given the class name 'hide'. This functionality seems to work perfectly in Chrome and Firefox, as the hidden options ...

JavaScript: Dynamic animation of a DIV element based on conditions

I am looking to create an animation for a DIV layer when another div is clicked. However, I only want the animation to play if the DIV layer has not been animated yet. My initial thought was to check the height value, as I am animating the height of the d ...

What is the method for retrieving the second data variable from a $.ajax() request in JavaScript?

I'm having an issue with my ajax function. I am trying to console.log the first data variable, but the code below isn't working: $('.comment_form').on('submit', function(e) { e.preventDefault(); $.ajax({ type: ...

I can't seem to get anything to show up on my React

After starting to work with routing on react.JS, I encountered a challenge where the simple products.jsx file is supposed to display a simple message upon clicking, but it's not showing anything. Here is the code from Index.JS import React from &apos ...

Guide on how to select a specific button from a set of buttons in JQuery without using a class name or id

Is it possible to select a specific button from a group of buttons using jQuery without utilizing a class or id? For example: <body> <button>Click Me</button> <button>Click Me</button> <button class="food"& ...

What is the reason for calling Proxy on nested elements?

Trying to organize Cypress methods into a helper object using getters. The idea is to use it like this: todoApp.todoPage.todoApp.main.rows.row .first().should('have.text', 'Pay electric bill'); todoApp.todoPage.todoApp.main.rows.ro ...

Tips for receiving alerts on external updates to a mongo collection using sails

Currently, I am in the process of creating an application utilizing mongo and sails. As part of my development, I am investigating how the real-time update feature in sails functions. Although I am currently using sails 0.9.16, I am also curious about ans ...

Tips for creating a sequence pattern in JavaScript or jQuery resembling 12345-1234567-8

I am looking to adjust this code so that a hyphen "-" is automatically inserted after every 5 characters, and then again after every 7 characters. Currently, the code only inserts a hyphen after every 5 characters. <html> <head> <scri ...

jQuery allows you to easily load a page fragment using Ajax

Below is the script I created to load a specific page using ajax. $(function(){ // Keep a mapping of url-to-container for caching purposes. var cache = { // If url is '' (no fragment), display this div's content. ...

Activating/Deactivating Textbox on Internet Explorer 9 using Javascript

It can't be due to browser discrepancies because I also tested it in Chrome. Is there possibly an error in my code? The checkbox is supposed to disable the textbox when clicked, but that's not happening. Here is the code I am using: I placed the ...

In IE7/8, the format property or method is not supported by the object

While using the Knob - jQuery Plugin, I encountered an error message in IE 7/8 Browser and Document Mode stating "Object doesn't support property or method 'format'". Below is a segment of the code that triggered the error: // Dial logic va ...

Is it possible to use ES6 import and require in Typescript 2?

In my Angular2 app, I have a class exported using ES6 modules: //File = init.todos.ts export class Init { load() { ... } } I am importing this class into another component like this: //File = todo.service.ts import { Init } from '. ...

Is my Basic RESTful API having trouble with missing rawBody in body-parser and the .delete operation not functioning properly?

I've been working on creating a basic RESTful API using express4, mongoose, and body parser as middleware. Index.js // Setting up the base var express = require('express'); var mailsystem = express(); var bodyParser = require('body- ...

Is there a way to create a new prettyphoto window by clicking on a link within the original prettyphoto window?

I have an HTML table that is dynamically constructed on the server side using AJAX. The table is displayed using prettyphoto, everything works fine up to this point. However, the last column of the table contains a link that should open an image using pret ...

Provide alternative styling through css in instances where the Google Books API does not provide an image

How can I modify the CSS code to display the author and title of a book when there is no image available from the Google Books API? Currently, users see a custom image linked here, but it's not clear what it represents without the name and author inf ...

Ways to activate the autosave feature in a NumericTextbox using Kendo UI scrollbars

Seeking guidance on implementing autosave functionality in a Kendo numeric textbox. I have created a demo where autosave works with a regular input text (autosave triggers when focus is lost or no values are entered for 2 seconds). However, when I switch ...