From 11683e29a0194d7568c15eda8ad0c9bb5920d8c8 Mon Sep 17 00:00:00 2001 From: Shahar Talmi Date: Fri, 27 Nov 2015 23:26:35 +0200 Subject: [PATCH] feat(ngView): reference resolved locals in scope this will make it easier to use ng-route with component pattern by being able to configure something like `$routeProvider.when('/', {resolve: {items: ($http) => $http.get(...)}, template: ''});` and not having to configure a dummy controller that puts the resolved values on the scope. --- src/ngRoute/directive/ngView.js | 1 + test/ngRoute/directive/ngViewSpec.js | 41 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/ngRoute/directive/ngView.js b/src/ngRoute/directive/ngView.js index f26ffbb3df5e..efb291392793 100644 --- a/src/ngRoute/directive/ngView.js +++ b/src/ngRoute/directive/ngView.js @@ -275,6 +275,7 @@ function ngViewFillContentFactory($compile, $controller, $route) { $element.data('$ngControllerController', controller); $element.children().data('$ngControllerController', controller); } + scope[current.resolveAs || '$resolve'] = locals; link(scope); } diff --git a/test/ngRoute/directive/ngViewSpec.js b/test/ngRoute/directive/ngViewSpec.js index 39ab1a934fea..bb5a64951fa7 100644 --- a/test/ngRoute/directive/ngViewSpec.js +++ b/test/ngRoute/directive/ngViewSpec.js @@ -120,6 +120,47 @@ describe('ngView', function() { }); + it('should reference resolved locals in scope', function() { + module(function($routeProvider) { + $routeProvider.when('/foo', { + resolve: { + name: function() { + return 'shahar'; + } + }, + template: '
{{$resolve.name}}
' + }); + }); + + inject(function($location, $rootScope) { + $location.path('/foo'); + $rootScope.$digest(); + expect(element.text()).toEqual('shahar'); + }); + }); + + + it('should allow to provide an alias for resolved locals using resolveAs', function() { + module(function($routeProvider) { + $routeProvider.when('/foo', { + resolveAs: 'myResolve', + resolve: { + name: function() { + return 'shahar'; + } + }, + template: '
{{myResolve.name}}
' + }); + }); + + inject(function($location, $rootScope) { + $location.path('/foo'); + $rootScope.$digest(); + expect(element.text()).toEqual('shahar'); + }); + }); + + it('should load content via xhr when route changes', function() { module(function($routeProvider) { $routeProvider.when('/foo', {templateUrl: 'myUrl1'});