質問

I have a simple resolve function set up in my route provider. Something like

.config(['$routeProvider', 'Constants', function config($routeProvider, Constants) {
    $routeProvider.
        when('mypage', {
            templateUrl: 'partials/mypage.tpl.html',
            controller: 'MyPageCtrl',
            reloadOnSearch: false,
            resolve: {
                factory: 'MyPageResolve'
            }
        })
)];

Where that factory is declared in a module like this:

.factory('MyPageResolve', ['$q', '$log', 'someAPI',
    function ($q, $log, someAPI) {
        var defer = $q.defer();
        var postUserFunc = function () {

            someAPI.get(
                function(data, status) { //$http.get().success() func
                    defer.resolve();
                }
            );

        };
        return defer.promise;
}]);

What's happening is on straight refresh or on the first load, this resolve method is hit. However, if I have a $location.url('/mypage'); change from one route to this one, angular is not re-resolving this controller.

I might be not thinking of resolve in the right way, and if so, would take recommendations on the 'angular' way to do this. I would like all this information loaded before the view, and is variable based on the route params.

役に立ちましたか?

解決

Ends up I needed to make that factory into just a function. A factory acts as a singleton, so it will only run once.

So I made my factory look like:

var myPageResolve = ['$q', '$log', 'someAPI',
function ($q, $log, someAPI) {
    var defer = $q.defer();
    var postUserFunc = function () {

        someAPI.get(
            function(data, status) { //$http.get().success() func
                defer.resolve();
            }
        );

    };
    return defer.promise;
}]);

And instead of resolving on 'myPageResolve', I resolved on myPageResolve. Very subtle change made all the difference.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top