The UI-router activates the child state prior to resolving the parent state

Currently, I am encountering an issue with executing initialization code within a parent state of the Angular ui-router. The initialization code in question is orderService.getStoreInfo(), which returns a promise. My intention is to trigger the child state only after this promise has been resolved. However, it seems that the child state is being triggered prior to the resolution of the parent's promise. Can you please help me identify what might be incorrect with my approach? Below is the relevant snippet of my code:

function configure($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/');

    $stateProvider
        .state('home', {
            abstract: true,
            template: '<ui-view/>',
            resolve: {
                storeInfo: /* @ngInject */ function(orderService) {
                    console.log('home: resolve - orderService.getStoreInfo()');
                    return orderService.getStoreInfo();
                }
            }
        })

        .state('home.orders', {
            url: '^/',
            template: '<my-order-list data-orders="vm.orders"></my-order-list>',
            resolve: {
                orders: /* @ngInject */ function (orderService) {
                    console.log('home.orders: resolve - orderService.getOrders()');
                    return orderService.getOrders();
                }
            },
            controller: ['orders', function (orders) {
                this.orders = orders;
            }],
            controllerAs: 'vm',
        });
}

The console log output for the above code is as follows:

home: resolve - orderService.getStoreInfo()
home.orders: resolve - orderService.getOrders()
... messages from orderService.getStoreInfo() ...

Based on the console log, it is evident that the child state is being activated before the resolution of the parent state. This is not the desired behavior.

Answer №1

The solution can be found here: it is crucial to inject the `resolve` keys into the child states for ensuring that promises are resolved before instantiating the children components. Therefore, make sure to explicitly include `storeInfo` in a resolve function of the child state.

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

Implement an event listener to detect incoming SMS messages using the mozMobileMessage API within the Boot to Gecko (B

As part of a university assignment, I have embarked on the journey of learning how to develop apps using AngularJS for Firefox OS. This marks my maiden voyage into the realm of JS app development. The primary objective of this app is to execute commands t ...

What are the restrictions of using the Angular orderby array function?

Is there a restriction on the number of values in an Angular orderby predicate? I am having trouble getting anything with more than 2 fields to function properly. It seems that anything after the 2nd index does not work. However, if I rearrange the fields ...

Tips for incorporating HTML code within a select option value?

I am working with AngularJS to create a Visual Composer for a website. One feature I want to incorporate is the ability to add HTML code based on the selection made in a dropdown menu. However, I am struggling to figure out how to include the HTML within t ...

Converting HTML code to JSON using PHP scripting

Would appreciate your help in resolving a small issue. Although I came across this post, I am still encountering some errors: How can I convert HTML to JSON using PHP? I have created a PHP file that extracts a post from WordPress with the following forma ...

Using AngularJS to update attributes in an HTML tag within a string

My string contains HTML tags : var str = "<div><p></p><p><i>blabla</i></p><p><i><b>blaaaaaablaaaaa</b></i></p><iframe src='urlAAA' height='400' width=&ap ...

When placed above in the template, the ng-model is causing the ng-repeat to not display anything

Currently, I am teaching myself Angular by following a tutorial at https://docs.angularjs.org/tutorial, but with my twist of using different data to keep things interesting. My struggle seems to stem from a simple mistake I might be making, compounded by ...

Is the ng-style not displaying the background image with the interpolated value?

I have been attempting to update the background image of a div using angular ng-style. Below is the code I am working with: <div class="cover-image" ng-style="{'background-image' : 'url({{data.image}})'}"></div> However, ...

Angular: ng-init does not initialize on page load

I've come across a variety of examples on stack overflow addressing the ng-init issue, but none seem to cover using it in conjunction with a controller. In my HTML file, I call the function in the controller like this: <div class="tab-container" ...

Accessing the parent scope from a directive within a nested ng-repeat in AngularJs

Seeking guidance on accessing the parent scope within a directive nested in an ng-repeat. Here is an example: <div ng-app="myApp" ng-controller="myCtrl"> <div ng-repeat="section in sections"> {{section.Name}} <div ng-rep ...

Mastering the art of horizontal scrolling within an angular material layout=row

I am attempting to create a slider using only a horizontal scrollbar with cards inside. In order to do this, I am trying to scroll the layout="row" within an ng-repeat. Check out my codepen here <body ng-app="myApp" ng-cloak ng-controller="ProductCont ...

Troubleshooting AngularJS: Diagnosing Issues with My Watch Functionality

I need to set up a watch on a variable in order to trigger a rest service call whenever its value changes and update the count. Below is the code snippet I have: function myController($scope, $http) { $scope.abc = abcValueFromOutsideOfMyController; ...

AngularJS: Functions may return false due to the asynchronous nature of services

Template Overview: <div data-ng-if="budgetSummaryExists()"> <span>You currently have no budget.</span> <button type="button" class="btn btn-danger" data-ng-click="setRoute('budgets')">Creat ...

Tools needed for Angular project development

Currently, I am engaged in a project using AngularJS on a Windows 7 computer. My desire is to be able to seamlessly transition between working at the office and working from home or any other location. If I have everything installed in a folder named C:/C ...

A guide on retrieving JSON data from an AJAX request using Angular UI Bootstrap

Greetings Friends, I have experience with traditional JavaScript and a little bit of JQuery. Now, for my new JAVA Web Based project, we need to utilize the latest technology to quickly build dynamic web pages. I don't have time to test all the widget/ ...

Remove dynamically created elements from an AngularJS div

Is there a way to remove an item from the criteria list when clicking the delete button? Currently, only the filter is being refreshed and not the tables. Any suggestions on how to resolve this issue? HTML <ul class="list-unstyled"> <l ...

Troubleshooting AngularJS Get Function Failure

New to the world of AngularJS, I find myself faced with a challenge. My .NET MVC WebAPI Restful app is up and running on an IIS server. However, when I attempt to query the API using , the response I receive is: [{"Id":1,"Name":"Glenn Block","Created":"00 ...

Instructions on how to insert a single parenthesis into a string using Angular or another JavaScript function

Currently, I am employing Angular JS to handle the creation of a series of SQL test scripts. A JSON file holds various test scenarios, each scenario encompassing a set of projects to be tested: $scope.tests = [ { "Date": "12/31/2017", "Project": ...

Setting up the current user's location when loading a map with Angular-google-maps

I am currently utilizing the library in conjunction with the IONIC framework. To manually set the center of the map, I have implemented the following code snippet: .controller('mainCtrl', function($scope) { $scope.map = { cen ...

The loading of script files is not working properly in an AngularJS web API application

After developing an application in angularjs that utilizes angular routing for calling web api services, everything functions properly when run from visual studio. However, upon deploying the application on an IIS web server, the script files fail to load ...

Sharing AngularJs controllers between different modules can help streamline your

I'm facing an issue with trying to access an array from one controller in another controller. Despite simplifying the code for clarity, I still can't seem to make it work. Here is my first controller: app.controller('mycont1', [' ...