Skip to content

Commit 04d02d0

Browse files
committed
$state.href() returns null instead of throwing.
1 parent c4d1403 commit 04d02d0

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

src/state.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
269269
$state.href = function (stateOrName, params, options) {
270270
options = extend({ lossy: true }, options || {});
271271
var state = findState(stateOrName);
272-
var nav = options.lossy ? state.navigable : state;
273-
274-
if (!nav || !nav.url) throw new Error("State '" + state + "' " + (
275-
options.lossy ? "does not have a URL or navigable parent" : "is not navigable"
276-
));
277-
return nav.url.format(normalize(state.params, params || {}));
272+
var nav = (state && options.lossy) ? state.navigable : state;
273+
return (nav && nav.url) ? nav.url.format(normalize(state.params, params || {})) : null;
278274
};
279275

280276
function resolveState(state, params, paramsAreFiltered, inherited, dst) {

src/stateDirectives.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ function $StateRefDirective($state) {
1818
if (newVal) params = newVal;
1919
if (!nav) return;
2020

21-
try {
22-
element[0][attr] = $state.href(ref.state, params, { lossy: true });
23-
} catch (e) {
21+
var newHref = $state.href(ref.state, params, { lossy: true });
22+
23+
if (!newHref) {
2424
nav = false;
25+
return false;
2526
}
27+
element[0][attr] = newHref;
2628
};
2729

2830
if (ref.paramExpr) {

test/stateSpec.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,8 @@ describe('state', function () {
256256

257257
describe('.href()', function () {
258258
it('aborts on un-navigable states', inject(function ($state) {
259-
expect(function() { $state.href("A"); }).toThrow(
260-
"State 'A' does not have a URL or navigable parent"
261-
);
262-
expect(function() { $state.href("about.sidebar", null, { lossy: false }); }).toThrow(
263-
"State 'about.sidebar' is not navigable"
264-
);
259+
expect($state.href("A")).toBeNull();
260+
expect($state.href("about.sidebar", null, { lossy: false })).toBeNull();
265261
}));
266262

267263
it('generates a parent state URL when lossy is true', inject(function ($state) {

0 commit comments

Comments
 (0)