Using the ngRoute Resolution Option with an Estimated Promise

I configured the resolvemultiple routes option to return the promise, to delay the creation of the controller instance until the promise is resolved. I am currently using function notation, instead of specifying a string for input.

For example:

.when('/article/:id', {
    templateUrl: 'app/article/article.html',
    controller: 'ArticleController',
    resolve: {
        article: ['Content', '$route', function (Content, $route) {
            return Content.get({ contentId: $route.current.params.id }).$promise;
        }]
    }
})

The parameter is articlecorrectly entered with the allowed promise value.

However, I would like to keep it DRY and enter this value by specifying the string and mentioned in the documentation .

When I set the factory function to provide such a promise, the instance is introduced only once (the first time). After that, the same promise is used because the AngularJS injection service cached the instance.

.factory('contentPromise', ['Content', '$route', function (Content, $route) {
    return Content.get({ contentId: $route.current.params.id }).$promise;
}])

, factory , ? ?

+4
1

, , - , , "DRYer":

.when('/article/:id', {
    ...
    resolve: {
        article: ['contentPromise', function (contentPromise) {
            return contentPromise();
        }]
    }
})

:

.factory('contentPromise', ['Content', '$route', function (Content, $route) {
    return function() {
        return Content.get({ contentId: $route.current.params.id }).$promise;
    };
}])
+7

Source: https://habr.com/ru/post/1526587/


All Articles