Issue with AngularJS ui-router failing to resolve a service call

I am facing an issue while trying to implement the resolve feature in my ui-router state provider. Here is how I have configured it:

app.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function ($stateProvider, $urlRouterProvider, $locationProvider) {
    ...
    $stateProvider.state('expositions', {
            url: '/Expositions',
            views: {
                "container": {
                    templateUrl: '/views/expositions.html', 
                    resolve: {
                        expositions: function ($http) {
                            return $http.get('/api/site/expositions').then(
                                function (response) {
                                    console.log("DATA", response.data);
                                    return response.data;
                                },
                                function (error) {
                                    console.log("ERROR", error);
                                })
                        }
                    }
                }
            }
        })
    ...
    }
    

When clicking on the expositions link, the resolve function is not triggered and the navigation does not happen.

I also attempted to set it up using a factory (resources service) like this:

app.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function ($stateProvider, $urlRouterProvider, $locationProvider) {
    ...
    $stateProvider.state('expositions', {
            url: '/Expositions',
            views: {
                "container": {
                    templateUrl: '/views/expositions.html', 
                    resolve: {
                        expositions: function (Resources) {
                            console.log("DATA", Resources);
                            return Resources.expositions();
                        }
                    }
                }
            }
        })
    ...
    }
    

The resource service is defined in a separate file as shown below:

resourcesService.js

(function () {
        'use strict';
    
        angular
            .module('site')
            .factory('Resources', ['$resource',
                function ($resource) {
                    return $resource('/api/site/:action/:id', {}, {
    
                        ...
    
                        expositions: {
                            method: 'GET',
                            params: { action: "expositions" },
                            isArray: true
                            //transformResponse: function (data) {
                            //    return angular.fromJson(data).list
                            //}
                        },
    
                        ...
                    });
                }
            ]);
    })();
    

Even with this setup, the resolve function is not being invoked. What could be the issue?

Appreciate any help provided!

Answer №1

Consider trying the following:

expositions: ['$http',function ($http) {

You could modify it to look like this:

resolve: {
                expositions: ['$http',function ($http) {
                    return $http.get('/api/site/expositions').then(
                        function (response) {
                            console.log("DATA", response.data);
                            return response.data;
                        }],
                        function (error) {
                            console.log("ERROR", error);
                        })
                }

Feel free to let me know if this resolves the issue for you

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

Integrating an Angular directive to include a cshtml page

Struggling with Angular to incorporate a partial cshtml view outside of ng-view. Main view: <div class="container"> <div class="header"> <div class="loginView"> <div ng-include="Home/Template/login"></di ...

Issue with Ext JS: The property 'substring' is being attempted to be read from an undefined value

Hey there! I'm trying to incorporate a grid panel into my view using Extjs 4.1.1, but I keep encountering an error in the browser console that says "Cannot read property 'substring' of undefined". Below is the JavaScript code snippet I am us ...

choosing a specific element within an HTML string stored in a variable

I have a variable containing HTML string data like this: var htmlstring = "<div><div class='testdiv'>924422</div></div>" My goal is to extract the content of the div with a specific class, in this case .testdiv. How ca ...

Using a customized layout, merge AngularJS into Vaadin

I experimented with integrating an angular JS application into Vaadin by utilizing a custom layout as shown below: VerticalLayout mainLayout = new VerticalLayout(); mainLayout.setMargin(true); mainLayout.setWidth("1380px"); setCompositionRoot( ...

How to overcome the issue of Vue.js mobile router-link being blocked by v-on:mouseover

Here is a for list snippet: <li class="projects-item" v-for="project in filteredProjects" :key="project.id" v-on:mouseover="displayHoverInfo($event, project)" v-on:mouseleave="hover = false" > <router-link v-bind:to="'/project/ ...

Is there a way to hide a paragraph or div using javascript?

I am experimenting with using buttons to hide paragraphs using javascript, but even when I set them as "hidden", there is still excess blank space left behind. Is there a way I can eliminate that extra space? Below is the javascript code: function backgro ...

Reading a JSON file using Javascript (JQuery)

Struggling to figure out how to handle a JSON file using JavaScript. Here are the details : { "streetCity": { "132":"Abergement-Clemenciat", "133":"Abergement-de-Varey", "134":"Amareins" } } I am attempting to access ...

employing the d3.queue method to defer execution until receiving the AJAX response

Seeking examples of integrating AJAX, d3, and PHP to extract data from a database and create graphs. Any guidance would be appreciated. I am currently using d3 to generate a force chart based on database information retrieved through AJAX and PHP. I have ...

"Using Angular and TypeScript to dynamically show or hide tabs based on the selected language on a website

When switching the language on the website, I want to display or hide a specific tab. If the language is set to German, then show the tab; if any other language is selected, hide it. Here's my code: ngOnInit(): void { this.translate.onLangChange.s ...

An asynchronized code block processes an array of multiple Ajax deferred calls, concluding with a synchronous final code block

I am new to the world of jQuery and AJAX and I'm currently exploring how deferrals and promises can help me solve a particular issue. Here's what I'm trying to achieve: I want to make calls to multiple URLs, process the returned results in p ...

Organize JSON data in Angular 6 from an observable based on a specific key

Note: While I am familiar with sorting a regular array of objects using .sort(), I am facing a challenge with an observable object that I am not accustomed to. My task involves retrieving a JSON array of objects with a service: import { Injectable } from ...

The selected attribute does not function properly with the <option> tag in Angular

Currently, I am faced with a challenge involving dropdowns and select2. My task is to edit a product, which includes selecting a category that corresponds to the product. However, when I open the modal, the selected group/category is not displayed in the d ...

Learn how to effectively utilize templateURL in an express and angular project

Our project utilizes Express without any view engine. To set up static directories, we have the following: app.use(express.static(__dirname + '/public')); app.use(express.static(__dirname + '/view')); app.use(express.static(__dirname + ...

Conceal a table row (tr) from a data table following deletion using ajax in the CodeIgniter

I am attempting to remove a record in codeigniter using an ajax call. The delete function is functioning correctly, but I am having trouble hiding the row after deletion. I am utilizing bootstrap data table in my view. <script> function remove_car ...

Retrieve the request body in Node.js Express server-side code in order to receive an array passed from the front end

I am facing an issue where I need to save a user's array to mongo. When the data passes through the bridge and reaches the posts, it appears as a strange object with string versions of its indices. I am attempting to convert it back into a normal arra ...

Instructions for removing and recreating an input field along with its parent elements when the value of a Select tag is changed

I currently have a table with four fields, as illustrated below: Semester | Exam Status | GPA | Fee Status My query is regarding the behavior when changing the value in Exam_Status: I noticed that the Select tag-> does not clear automatically. Specifi ...

The functionality of CSS3 animations may sometimes be unreliable following the onLoad event

Here is a snippet of code to consider: $(function() { $('#item').css({ webkitTransform: 'translate(100px, 100px)' }); }); The element I am attempting to move has the following CSS properties: transform: translate3d(0 ...

Developing Java-based Ajax scripts

Is there a way to implement AJAX functionality in Java without actually using AJAX itself? I'm searching for a JAVA API that can achieve this. Just like we can submit data from a web page using a JAVA program, I want to handle AJAX operations through ...

Encountering the 401 (Unauthorized) error while attempting to delete data in loopback?

I have a model called companyUsers and when I send a GET request, I am able to retrieve all the data successfully. However, when I try to make a DELETE or PUT request, I encounter a 401 (Unauthorized) error. For example, I made a DELETE request using the ...

What is the reason for the initial DOM operation taking significantly longer than subsequent ones?

Why is the first operation much more despite the subsequent manipulation of the DOM being practically identical in time when both are written with the realization in JS and jQuery? Link to Javascript source // ES5 // Sending a message function sendMessa ...