Skip to content

Commit 5f6870d

Browse files
cristHian Gzchristopherthielen
cristHian Gz
authored andcommitted
fix(uiSref): avoid empty links (#26)
1 parent ccbc56f commit 5f6870d

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/directives/uiSref.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import { Subscription } from "rxjs/Subscription";
1414
export class AnchorUISref {
1515
constructor(public _el: ElementRef, public _renderer: Renderer) { }
1616
update(href: string) {
17-
this._renderer.setElementProperty(this._el.nativeElement, 'href', href);
17+
if (href && href != '') {
18+
this._renderer.setElementProperty(this._el.nativeElement, 'href', href);
19+
} else {
20+
this._el.nativeElement.removeAttribute('href');
21+
}
1822
}
1923
}
2024

test/uiSref.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Component, NO_ERRORS_SCHEMA } from "@angular/core";
2+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
3+
import { By } from '@angular/platform-browser';
4+
import { DebugElement } from '@angular/core';
5+
6+
import { UIRouterModule } from '../src/uiRouterNgModule';
7+
import { UISref } from '../src/directives/uiSref';
8+
9+
describe('uiSref', () => {
10+
describe('empty links', () => {
11+
@Component({
12+
template: `
13+
<a [uiSref]="null"></a>
14+
<a [uiSref]="''"></a>
15+
`
16+
})
17+
class TestComponent { }
18+
19+
let des: DebugElement[];
20+
let comp: TestComponent;
21+
let fixture: ComponentFixture<TestComponent>;
22+
23+
beforeEach(() => {
24+
fixture = TestBed.configureTestingModule({
25+
declarations: [ TestComponent ],
26+
imports: [ UIRouterModule ]
27+
}).createComponent(TestComponent);
28+
fixture.detectChanges();
29+
des = fixture.debugElement.queryAll(By.directive(UISref));
30+
});
31+
32+
it('should not bind "null" string to `href`', () => {
33+
expect(des[0].nativeElement.hasAttribute('href')).toBeFalsy();
34+
expect(des[1].nativeElement.hasAttribute('href')).toBeFalsy();
35+
});
36+
});
37+
});
38+

0 commit comments

Comments
 (0)