Issues with Angular-UI-router resolve functionality not functioning as expected

I have encountered a problem in my Angular-ui-router where the use of resolve is causing my site to send out multiple get requests per second without loading the view. My stack consists of the full MEAN-stack.

The views are generated using inline templates, for example:

    <script type="text/ng-template" id="/home.html">......</script>

These templates are then used within a <ui-view> tag.

Please review this code snippet:

app.factory('posts', ['$http', function($http) {
    var o = {
        posts: []
    };
    o.getAll = function() {
      return $http.get('/posts').success(function(data){
        angular.copy(data, o.posts);
      });
    };
    return o;
}]);

app.config([
    '$stateProvider',
    '$urlRouterProvider',

    function($stateProvider, $urlRouterProvider) {
        $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: '/home.html',
            controller: 'MainCtrl',
            resolve : {
                postPromise : ['posts',
                function(posts) {
                    return posts.getAll();
                }]
            }
        })

If I remove the Resolve section, the page loads fine.

EDIT: After further investigation, I discovered that the code I wrote was intended for angular version <= 1.4 and therefore I need to use .then instead of .success. However, when I switch to using .then, I lose the persistent data functionality that the factory is supposed to provide.

My new question is whether I should stick with AngularJS 1.4 or seek assistance in converting it to 1.6? Your input is greatly appreciated, thank you.

Answer №1

Try storing the $http promise object in a variable before returning it, like this:

 var promise = $http.get('posts');
     promise.then(function(response) {

            },
        function(data) {

            });
 return promise;

Keep in mind that using success and error callbacks are no longer supported in angular 1.6, so make sure to use then.

  1. Check out the documentation for more information.

  2. Explore other useful resources on Angular 1.6.

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

Tips for ensuring the directive stays current with changes to the parent scope variable

Example: <div ng-show="{{ display }}"> ... </div> angular.module('mymodule').directive('mydirective', [ function () { return { scope: { display: '=' }, ... }; ...

The CSS transition fails to function correctly when rendering a React element within an array

When rendering a React component within an array using CSS transitions, I noticed that the elements in the array re-order and change style. Surprisingly, only the elements moving up have transitions applied, while the ones moving down do not. I expect all ...

Guide to creating fog animations in three.js

I'm attempting to adjust the fog density using tweening, but for some reason, it doesn't seem to be working. Here are my default settings: var camera, densityFog, colorFog2; colorFog2 = 0xfee2ed; densityFog ...

How do I use onclick to hide a <div> and reveal the content beneath it?

Is there a way for me to hide this <div> when the user clicks outside of it? I want the content behind it to be visible once the <div> is hidden. <html> <style> .overlay { position: fixed; top: 0; left: 0; height: ...

Fetching server-side data for isomorphic React app: A guide to accessing cookies

Currently, I am in the process of developing an isomorphic/universal React + Redux + Express application. The method I use for server-side data fetching is quite standard: I identify the routes that match the URL, call the appropriate data-fetching functio ...

Tips for utilizing my fiddle for subsequent data entry by incorporating new elements into the environment using three.js

Whenever I input a number for the second, third, etc. time in the textbox, I want to remove old objects and add new ones. Basically, I need to delete existing objects from the scene and then introduce new objects based on the new input. I have tried variou ...

The attachment content disposition is failing to initiate the download dialog

While working on setting up a file download feature on my NodeJS server, I came across some unexpected behavior. I have a REST (express) API that utilizes an export data function to generate a CSV file on the server. To initiate the download of this file, ...

OBJ Raycasting in Three.js

Greetings! I encountered an issue while working with three.js in my project. Specifically, I was trying to select a custom mesh that I loaded from an OBJ file. To troubleshoot, I set up a simple raycaster, a cube, and my custom model (which is also a cube ...

Issue with jQuery .on('change') function not functioning correctly following a row being duplicated

Within my input form, I have implemented an on-click function that triggers an ajax call to submit a table row to the database. Upon submission, the row is then duplicated so that the user can input more data if necessary. The issue I am currently facing r ...

Adjusting the D3 SVG zoom to focus on a specific rectangle within a group (<g>) at a varying angle

I am working with an <svg> that contains background images and various rectangles positioned correctly within the svg. The rectangles are grouped using <g> tags with translation values and specific rotation angles assigned to each one. My goal ...

How to Submit a Form in jQuery with both File and Text inputs without Redirecting the Current Window

I am working on creating an application form that allows users to attach images. The form consists of a few input elements placed within a div rather than a traditional form element. I need the form to be submitted via AJAX without causing a page redirect, ...

Using the Mousetrap binding on a nuxt.js page may not be effective

Hey there! I'm trying to achieve a certain functionality where I want to redirect to a different page once a specific sequence is typed. Although I can see the message "It works" in my console, the redirection is not happening and instead, I am gettin ...

Initiate an asynchronous request from JavaScript to a C# controller located in a separate directory

Note: Updated at the bottom of question I'm encountering difficulties with making an AJAX call from JavaScript to the C# Controller. The issue seems to be related to the connection URL in my AJAX call within the JavaScript file. If the URL isn't ...

Utilizing Vue and Websockets for seamless communication: Managing the exchange of messages between users

In an attempt to create a messaging system that shows alerts of messages to different users, I have implemented Vue Socket Io from https://www.npmjs.com/package/vue-socket.io. However, the issue lies in the fact that the alerts are not being triggered as e ...

Guide on downloading the complete Vue.js to-do list with JsPdf

I am new to this field and attempting to create a small project using Vue.js. I want to provide users with the option to download their Vue.js to-do list. I have imported the jspdf library and Html2canvas. Here is the current output, but this is the desire ...

Ensure Rxjs waits for the completion of the previous interval request before moving forward

Scenario: It is required to make an API call every 3 minutes to update the status of a specific service within the application. Here is my existing code snippet: interval(180000) .subscribe(() => this.doRequest ...

Having trouble uploading a file in Express JS with Multer?

I'm currently working on developing an API using express JS. Specifically, I have a router set up for uploading images using the multer package. This is my router configuration: const multer = require('multer'); module.exports = (app) =&g ...

Generate a backup link for when the app is not installed by utilizing JavaScript

I am in the process of developing a plugin that will transform regular links into deep links for users on Android or iOS devices. For example, it will identify the link and replace it with a deep link like this: var facebookURL = $('[href*="facebook ...

Developing components through JavaScript

I am attempting to use JavaScript to create an element in my HTML using createElement, but there seems to be an issue in my code. HTML <button class="test_button">I am a test</button> <div class="test"></div> ...

The YouTube Iframe API encountered an issue while attempting to send a message to an HTTP recipient. The recipient's origin

Encountering an error when using the YouTube Iframe API on Safari 9.1, OS X Yosemite Issue: Unable to post message to http://www.youtube.com. Recipient has origin https://www.youtube.com This problem only occurs in Safari, as other browsers like Firefo ...