Skip to content

Commit 691745b

Browse files
fix(uiSref): Generate an href for states with a blank url. closes #1293
1 parent 9c88207 commit 691745b

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

src/stateDirectives.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function $StateRefDirective($state, $timeout) {
108108
if (activeDirective) {
109109
activeDirective.$$setStateInfo(ref.state, params);
110110
}
111-
if (!newHref) {
111+
if (newHref === null) {
112112
nav = false;
113113
return false;
114114
}

src/urlRouter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
384384
var url = urlMatcher.format(params);
385385
options = options || {};
386386

387-
if (!isHtml5 && url) {
387+
if (!isHtml5 && url !== null) {
388388
url = "#" + $locationProvider.hashPrefix() + url;
389389
}
390390
url = appendBasePath(url, isHtml5, options.absolute);

test/stateDirectivesSpec.js

+28-25
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ describe('uiStateRef', function() {
55
beforeEach(module('ui.router'));
66

77
beforeEach(module(function($stateProvider) {
8-
$stateProvider.state('index', {
9-
url: '/'
8+
$stateProvider.state('top', {
9+
url: ''
1010
}).state('contacts', {
1111
url: '/contacts',
1212
template: '<a ui-sref=".item({ id: 5 })" class="item">Person</a> <ui-view></ui-view>'
@@ -75,15 +75,17 @@ describe('uiStateRef', function() {
7575
});
7676

7777
describe('links', function() {
78-
var timeoutFlush;
78+
var timeoutFlush, el2;
7979

8080
beforeEach(inject(function($rootScope, $compile, $timeout) {
8181
el = angular.element('<a ui-sref="contacts.item.detail({ id: contact.id })">Details</a>');
82+
el2 = angular.element('<a ui-sref="top">Top</a>');
8283
scope = $rootScope;
8384
scope.contact = { id: 5 };
8485
scope.$apply();
8586

8687
$compile(el)(scope);
88+
$compile(el2)(scope);
8789
scope.$digest();
8890

8991
timeoutFlush = function() {
@@ -98,6 +100,7 @@ describe('uiStateRef', function() {
98100

99101
it('should generate the correct href', function() {
100102
expect(el.attr('href')).toBe('#/contacts/5');
103+
expect(el2.attr('href')).toBe('#');
101104
});
102105

103106
it('should update the href when parameters change', function() {
@@ -118,7 +121,7 @@ describe('uiStateRef', function() {
118121
}));
119122

120123
it('should transition states when left-clicked', inject(function($state, $stateParams, $q) {
121-
expect($state.$current.name).toEqual('');
124+
expect($state.$current.name).toEqual('top');
122125

123126
triggerClick(el);
124127
timeoutFlush();
@@ -129,7 +132,7 @@ describe('uiStateRef', function() {
129132
}));
130133

131134
it('should transition when given a click that contains no data (fake-click)', inject(function($state, $stateParams, $q) {
132-
expect($state.current.name).toEqual('');
135+
expect($state.current.name).toEqual('top');
133136

134137
triggerClick(el, {
135138
metaKey: undefined,
@@ -146,63 +149,63 @@ describe('uiStateRef', function() {
146149
}));
147150

148151
it('should not transition states when ctrl-clicked', inject(function($state, $stateParams, $q) {
149-
expect($state.$current.name).toEqual('');
152+
expect($state.$current.name).toEqual('top');
150153
triggerClick(el, { ctrlKey: true });
151154

152155
timeoutFlush();
153156
$q.flush();
154157

155-
expect($state.current.name).toEqual('');
156-
expect($stateParams).toEqual({ id: 5 });
158+
expect($state.current.name).toEqual('top');
159+
expect($stateParams).toEqualData({ });
157160
}));
158161

159162
it('should not transition states when meta-clicked', inject(function($state, $stateParams, $q) {
160-
expect($state.$current.name).toEqual('');
163+
expect($state.$current.name).toEqual('top');
161164

162165
triggerClick(el, { metaKey: true });
163166
timeoutFlush();
164167
$q.flush();
165168

166-
expect($state.current.name).toEqual('');
167-
expect($stateParams).toEqual({ id: 5 });
169+
expect($state.current.name).toEqual('top');
170+
expect($stateParams).toEqualData({});
168171
}));
169172

170173
it('should not transition states when shift-clicked', inject(function($state, $stateParams, $q) {
171-
expect($state.$current.name).toEqual('');
174+
expect($state.$current.name).toEqual('top');
172175

173176
triggerClick(el, { shiftKey: true });
174177
timeoutFlush();
175178
$q.flush();
176179

177-
expect($state.current.name).toEqual('');
178-
expect($stateParams).toEqual({ id: 5 });
180+
expect($state.current.name).toEqual('top');
181+
expect($stateParams).toEqualData({});
179182
}));
180183

181184
it('should not transition states when middle-clicked', inject(function($state, $stateParams, $q) {
182-
expect($state.$current.name).toEqual('');
185+
expect($state.$current.name).toEqual('top');
183186

184187
triggerClick(el, { button: 1 });
185188
timeoutFlush();
186189
$q.flush();
187190

188-
expect($state.current.name).toEqual('');
189-
expect($stateParams).toEqual({ id: 5 });
191+
expect($state.current.name).toEqual('top');
192+
expect($stateParams).toEqualData({});
190193
}));
191194

192195
it('should not transition states when element has target specified', inject(function($state, $stateParams, $q) {
193196
el.attr('target', '_blank');
194-
expect($state.$current.name).toEqual('');
197+
expect($state.$current.name).toEqual('top');
195198

196199
triggerClick(el);
197200
timeoutFlush();
198201
$q.flush();
199202

200-
expect($state.current.name).toEqual('');
201-
expect($stateParams).toEqual({ id: 5 });
203+
expect($state.current.name).toEqual('top');
204+
expect($stateParams).toEqualData({});
202205
}));
203206

204207
it('should not transition states if preventDefault() is called in click handler', inject(function($state, $stateParams, $q) {
205-
expect($state.$current.name).toEqual('');
208+
expect($state.$current.name).toEqual('top');
206209
el.bind('click', function(e) {
207210
e.preventDefault();
208211
});
@@ -211,8 +214,8 @@ describe('uiStateRef', function() {
211214
timeoutFlush();
212215
$q.flush();
213216

214-
expect($state.current.name).toEqual('');
215-
expect($stateParams).toEqual({ id: 5 });
217+
expect($state.current.name).toEqual('top');
218+
expect($stateParams).toEqualData({});
216219
}));
217220

218221
it('should allow passing params to current state', inject(function($compile, $rootScope, $state) {
@@ -345,8 +348,8 @@ describe('uiSrefActive', function() {
345348
beforeEach(module('ui.router'));
346349

347350
beforeEach(module(function($stateProvider) {
348-
$stateProvider.state('index', {
349-
url: '',
351+
$stateProvider.state('top', {
352+
url: ''
350353
}).state('contacts', {
351354
url: '/contacts',
352355
views: {

0 commit comments

Comments
 (0)