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

Commit 11683e2

Browse files
author
Shahar Talmi
committed
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: '<my-app items="$resolve.items" />'});` and not having to configure a dummy controller that puts the resolved values on the scope.
1 parent 73e3865 commit 11683e2

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)