Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ui router resolve depends on other resolve

Tags:

angularjs

I got this resolve in my state

first I get the test into a variable, and then I want to get the test2 variable, but in order to get test2 I have to wait for test again.

Is there a cleverer way of doing this?

Thank you

resolve: {
        one: function (test) {
            return test.gettest().$promise.then(data => {
                return data;
            });
        },
        two: function (test, test2, $stateParams) {
            return test.gettest().$promise.then(test=> {

                return test2.gettest2(test.id, $stateParams.id).$promise.then(data => {
                    return data;
                });

            });
        }
    },
like image 910
Ørnulf Arntsen Avatar asked Mar 10 '23 09:03

Ørnulf Arntsen


1 Answers

If you inject the first resolve variable (one) into the second resolve statement, angular ui router is smart enough to realize that it will not run the second resolve statement until the first one has completed. So what you would do looks something like below:

resolve: {
    one: function (test) {
        return test.gettest().$promise.then(data => {
            return data;
        });
    },
    two: function (one, test2, $stateParams) {
        //one is equal to the result of test.gettest()
        return test2.gettest2(one.id, $stateParams.id).$promise.then(data => {
                return data;
            });

        });
    }
},
like image 65
fshaw1 Avatar answered Mar 29 '23 02:03

fshaw1