X Tutup
Skip to content

Commit 0f22dce

Browse files
petebacondarwinIgorMinar
authored andcommitted
feat(angular1_router): allow component to bind to router
1 parent c603643 commit 0f22dce

File tree

2 files changed

+65
-16
lines changed

2 files changed

+65
-16
lines changed

modules/angular1_router/src/ng_outlet.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,11 @@ function ngOutletDirective($animate, $q: ng.IQService, $router) {
156156

157157
this.controller.$$routeParams = instruction.params;
158158
this.controller.$$template =
159-
'<' + dashCase(componentName) + '></' + dashCase(componentName) + '>';
159+
'<' + dashCase(componentName) + ' router="$$router"></' + dashCase(componentName) + '>';
160160
this.controller.$$router = this.router.childRouter(instruction.componentType);
161161

162162
let newScope = scope.$new();
163+
newScope.$$router = this.controller.$$router;
163164

164165
let clone = $transclude(newScope, clone => {
165166
$animate.enter(clone, null, this.currentElement || element);

modules/angular1_router/test/integration/router_spec.js

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,84 @@ describe('router', function () {
4444
expect(elt.text()).toBe('Home');
4545
}));
4646

47-
function registerComponent(name, options) {
48-
var controller = options.controller || function () {};
4947

50-
['$onActivate', '$onDeactivate', '$onReuse', '$canReuse', '$canDeactivate'].forEach(function (hookName) {
51-
if (options[hookName]) {
52-
controller.prototype[hookName] = options[hookName];
53-
}
48+
it('should bind the component to the current router', inject(function($location) {
49+
var router;
50+
registerComponent('homeCmp', {
51+
bindings: { router: '=' },
52+
controller: function($scope, $element) {
53+
this.$routerOnActivate = function() {
54+
router = this.router;
55+
};
56+
},
57+
template: 'Home'
5458
});
5559

60+
registerComponent('app', {
61+
template: '<div ng-outlet></div>',
62+
$routeConfig: [
63+
{ path: '/', component: 'homeCmp' }
64+
]
65+
});
66+
67+
compile('<app></app>');
68+
69+
$location.path('/');
70+
$rootScope.$digest();
71+
var homeElement = elt.find('home-cmp');
72+
expect(homeElement.text()).toBe('Home');
73+
expect(homeElement.isolateScope().$ctrl.router).toBeDefined();
74+
expect(router).toBeDefined();
75+
}));
76+
77+
function registerDirective(name, options) {
5678
function factory() {
5779
return {
5880
template: options.template || '',
5981
controllerAs: name,
60-
controller: controller
82+
controller: getController(options)
6183
};
6284
}
85+
applyStaticProperties(factory, options);
86+
$compileProvider.directive(name, factory);
87+
}
6388

64-
if (options.$canActivate) {
65-
factory.$canActivate = options.$canActivate;
66-
}
67-
if (options.$routeConfig) {
68-
factory.$routeConfig = options.$routeConfig;
69-
}
89+
function registerComponent(name, options) {
7090

71-
$compileProvider.directive(name, factory);
91+
var definition = {
92+
bindings: options.bindings,
93+
template: options.template || '',
94+
controller: getController(options),
95+
}
96+
applyStaticProperties(definition, options);
97+
$compileProvider.component(name, definition);
7298
}
7399

74100
function compile(template) {
75101
elt = $compile('<div>' + template + '</div>')($rootScope);
76102
$rootScope.$digest();
77103
return elt;
78104
}
79-
});
105+
106+
function getController(options) {
107+
var controller = options.controller || function () {};
108+
[
109+
'$routerOnActivate', '$routerOnDeactivate',
110+
'$routerOnReuse', '$routerCanReuse',
111+
'$routerCanDeactivate'
112+
].forEach(function (hookName) {
113+
if (options[hookName]) {
114+
controller.prototype[hookName] = options[hookName];
115+
}
116+
});
117+
return controller;
118+
}
119+
120+
function applyStaticProperties(target, options) {
121+
['$canActivate', '$routeConfig'].forEach(function(property) {
122+
if (options[property]) {
123+
target[property] = options[property];
124+
}
125+
});
126+
}
127+
});

0 commit comments

Comments
 (0)
X Tutup