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

Commit 983b059

Browse files
Shahar Talmipetebacondarwin
Shahar Talmi
authored andcommitted
feat(ngView): reference resolved locals in scope
All the resolves for a route are now attached to the route's local scope, as the property whose name is given by the `resolveAs` property on the route definition. If `resolveAs` is not specified it defaults to `$resolve`. This will make it easier to use `ngRoute`, by being able to reference all the resolve values for the route, directly on the scope, rather than having to implement a controller just to copy the resolves across manually. For example, rather than ```js $routeProvider.when('/', { resolve: { item1: ($http) => $http.get(...), item2: ($http) => $http.get(...) }, template: '<my-app item1="vm.item1" item2="vm.item2">'</my-app>`, controllerAs: 'vm', controller: ['item1', 'item2', function(item1, item2) { this.item1 = item1; this.item2 = item2; }] }); ``` one can now do ```js $routeProvider.when('/', { resolve: { item1: ($http) => $http.get(...), item2: ($http) => $http.get(...) }, template: '<my-app item1="$resolve.item1" item2="$resolve.item2">'</my-app>` }); ``` BREAKING CHANGE: A new property is being attached to the scope of the route. The default name for this property is `$resolve`. If your scope already contains a property with this name then it will be hidden or overwritten. In this case, you should choose a custom name for this property, that will not collide with other properties on the scope, by specifying the `resolveAs` property on the route. Closes #13400
1 parent 25e8c59 commit 983b059

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/ngRoute/directive/ngView.js

+1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ function ngViewFillContentFactory($compile, $controller, $route) {
275275
$element.data('$ngControllerController', controller);
276276
$element.children().data('$ngControllerController', controller);
277277
}
278+
scope[current.resolveAs || '$resolve'] = locals;
278279

279280
link(scope);
280281
}

test/ngRoute/directive/ngViewSpec.js

+41
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,47 @@ describe('ngView', function() {
120120
});
121121

122122

123+
it('should reference resolved locals in scope', function() {
124+
module(function($routeProvider) {
125+
$routeProvider.when('/foo', {
126+
resolve: {
127+
name: function() {
128+
return 'shahar';
129+
}
130+
},
131+
template: '<div>{{$resolve.name}}</div>'
132+
});
133+
});
134+
135+
inject(function($location, $rootScope) {
136+
$location.path('/foo');
137+
$rootScope.$digest();
138+
expect(element.text()).toEqual('shahar');
139+
});
140+
});
141+
142+
143+
it('should allow to provide an alias for resolved locals using resolveAs', function() {
144+
module(function($routeProvider) {
145+
$routeProvider.when('/foo', {
146+
resolveAs: 'myResolve',
147+
resolve: {
148+
name: function() {
149+
return 'shahar';
150+
}
151+
},
152+
template: '<div>{{myResolve.name}}</div>'
153+
});
154+
});
155+
156+
inject(function($location, $rootScope) {
157+
$location.path('/foo');
158+
$rootScope.$digest();
159+
expect(element.text()).toEqual('shahar');
160+
});
161+
});
162+
163+
123164
it('should load content via xhr when route changes', function() {
124165
module(function($routeProvider) {
125166
$routeProvider.when('/foo', {templateUrl: 'myUrl1'});

0 commit comments

Comments
 (0)