From 12961d4a50d3e5bab06583a74aeab0f4220634a6 Mon Sep 17 00:00:00 2001 From: Brandon Date: Tue, 26 Apr 2016 14:46:41 -0500 Subject: [PATCH] fix(LinkActive): Fix active link for base url when using hash (#74) --- lib/link-active.ts | 14 +++++++------- spec/link-active.spec.ts | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/link-active.ts b/lib/link-active.ts index 749529e..3aa1b0d 100644 --- a/lib/link-active.ts +++ b/lib/link-active.ts @@ -43,7 +43,7 @@ export interface LinkActiveOptions { ngAfterViewInit() { this._sub = this.router$ - .map(({path}) => this.router$.prepareExternalUrl(path)) + .map(({path}) => this.router$.prepareExternalUrl(path || '/')) .subscribe(path => { this.checkActive(path); }); @@ -53,12 +53,12 @@ export interface LinkActiveOptions { let active = this.links.reduce((active, current) => { let [href, query] = current.linkHref.split('?'); - if (this.activeOptions.exact) { - return active ? active : href === path; - } else { - return active ? active : path.startsWith(href); - } - }, false); + if (this.activeOptions.exact) { + return active ? active : href === path; + } else { + return active ? active : path.startsWith(href); + } + }, false); let activeClasses = this.activeClass.split(' '); activeClasses.forEach((activeClass) => { diff --git a/spec/link-active.spec.ts b/spec/link-active.spec.ts index 58201d2..df89e7f 100644 --- a/spec/link-active.spec.ts +++ b/spec/link-active.spec.ts @@ -169,4 +169,23 @@ describe('Link Active', () => { expect(link.getAttribute('class')).toEqual('active'); }); }))); + + describe('With Hash', () => { + it('should add the provided class to the active element', async(inject([TestComponentBuilder, Router, LocationStrategy], (tcb, router$, ls) => { + ls.internalBaseHref = '#'; + + router$.next({ + path: '/' + }); + + return compile(tcb, 'Page') + .then((fixture) => { + fixture.detectChanges(); + let compiled = fixture.debugElement.nativeElement; + let link: Element = compiled.querySelector('a'); + + expect(link.getAttribute('class')).toEqual('active'); + }); + }))); + }); });