Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

docs($routeProvider): Document resolved values #11413

Closed
wants to merge 1 commit into from

Conversation

fredsa
Copy link
Contributor

@fredsa fredsa commented Mar 24, 2015

Document that resolved values are available in $route.current.locals

Document that resolved values are available in `$route.current.locals`
@petebacondarwin
Copy link
Contributor

In what situation are they available? If you want them in the controller then they are available via injection.

@fredsa
Copy link
Contributor Author

fredsa commented Mar 25, 2015

This is about the resolved promises provided in the 'resolve' map of the route.

@pkozlowski-opensource
Copy link
Member

@fredsa even if those are available, why not using DI for them (all the locals are injectable to controllers defined on a route level)? Is it about a "big" number of locals to be injected? If so maybe we should say in the docs when one way of doing things "wins" over another?

@fredsa
Copy link
Contributor Author

fredsa commented Mar 27, 2015

@pkozlowski-opensource AFAICT the resolved values are only available via $route.current.locals and not injectable.

Here's the code I'm using. Note the assignment of $scope.products from $route.current.locals.products in $routeChangeSuccess event handler:

.config(['$routeProvider', function($routeProvider) {
    $routeProvider
      .when('/products', {
        templateUrl: 'partial/products.html',
        resolve: {
          products: 'products_promise'
        },
        reloadOnSearch: false,
      })
})

.factory('products_promise', ['$http', function($http) {
  //fetch JSON from server
  return $http.get('/api/products')
  .then(function(result) {
    return result.data;
  });
})

.controller('ProductsController', ['$scope', '$route', function($scope, $route) {
  $scope.$on('$routeChangeSuccess', function(evt, current, previous) {
    $scope.products = $route.current.locals.products;
  });
}])

Note further:

// This doesn't work because there is no `products` service/factory:
.controller('ProductsController', ['$scope', '$route', 'products', function($scope, $route, products) {
  $scope.$on('$routeChangeSuccess', function(evt, current, previous) {
    $scope.products = products;
  });
}])

and:

// This doesn't work because `products_promise` is a promise, rather than the desired resolved value:
.controller('ProductsController', ['$scope', '$route', 'products_promise', function($scope, $route, products_promise) {
  $scope.$on('$routeChangeSuccess', function(evt, current, previous) {
    // Can't really use products_promise here because I want the resolved value
  });
}])

@gkalpak
Copy link
Member

gkalpak commented Mar 28, 2015

@petebacondarwin, @pkozlowski-opensource, are referring to controllers defined for the route (in which the resolved values can be injected).

@fredsa, is referring to injecting resolved dependencies to other controllers (e.g. a parent controller), in which case I think the route's locals is the only way.

Example of both

I believe that (although undocumented) locals is not supposed to be private or something, since there's no $$ prefix.

@gkalpak
Copy link
Member

gkalpak commented Mar 28, 2015

BTW, if we decide to document the ability to access resolved dependencies via locals, I think it shouldn't be related to resolved property, but a "stand-alone" note and an explanation is needed for when it makes sense to do so (e.g. in controllers not related to the route).

@petebacondarwin
Copy link
Contributor

I don't think that accessing resolves from an arbitrary controller is a good pattern to follow:

  • If you are not inside a route's template then it makes no sense to try to access these resolves since you can't be sure that the promises have been resolved.
  • If you are inside a template then it makes more sense to use a route controller, inject the resolves into this and then expose the values to the rest of the template for other controllers to access.

That being said, if you are trying to set up a global route change success handler, from inside a service, then I guess you might want to access the resolves. In this case you would indeed use $route.current.locals to access these values but this is a special case.

@petebacondarwin
Copy link
Contributor

So, agreeing with @gkalpak, I would rather document this in the $routeChangeSuccess event as this is the only place it is really relevant.

petebacondarwin added a commit that referenced this pull request Jul 13, 2015
netman92 pushed a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
ggershoni pushed a commit to ggershoni/angular.js that referenced this pull request Sep 29, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants