X Tutup
Skip to content

Commit 11e8aa2

Browse files
feat(angular1_router): Add ng-link-active class to active ng-link
Closes #6882
1 parent 81beb1c commit 11e8aa2

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

modules/angular1_router/src/ng_outlet.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,16 @@ function ngLinkDirective($rootRouter, $parse) {
257257

258258
function getLink(params) {
259259
instruction = router.generate(params);
260+
261+
scope.$watch(function() { return router.isRouteActive(instruction); },
262+
function(active) {
263+
if (active) {
264+
element.addClass('ng-link-active');
265+
} else {
266+
element.removeClass('ng-link-active');
267+
}
268+
});
269+
260270
return './' + angular.stringifyInstruction(instruction);
261271
}
262272

modules/angular1_router/test/ng_link_spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ describe('ngLink', function () {
135135
}).not.toThrow();
136136
});
137137

138+
it('should add an ng-link-active class on the current link', inject(function ($rootRouter) {
139+
$rootRouter.config([
140+
{ path: '/', component: 'oneCmp', name: 'One' }
141+
]);
142+
143+
compile('<a ng-link="[\'/One\']">one</a> | <div ng-outlet></div>');
144+
$rootScope.$digest();
145+
146+
$rootRouter.navigateByUrl('/');
147+
$rootScope.$digest();
148+
149+
expect(elt.find('a').attr('class')).toBe('ng-link-active');
150+
}));
151+
138152

139153
function registerComponent(name, template, config) {
140154
var controller = function () {};

0 commit comments

Comments
 (0)
X Tutup