fix(router): router-link works without params
Router-link attaches a listener to prevent default behavior and navigate. Closes: 1689
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import {RegExp, RegExpWrapper, RegExpMatcherWrapper, StringWrapper, isPresent} from 'angular2/src/facade/lang';
|
||||
import {RegExp, RegExpWrapper, RegExpMatcherWrapper, StringWrapper, isPresent, isBlank, BaseException} from 'angular2/src/facade/lang';
|
||||
import {Map, MapWrapper, StringMap, StringMapWrapper, List, ListWrapper} from 'angular2/src/facade/collection';
|
||||
|
||||
import {escapeRegex} from './url';
|
||||
@ -27,6 +27,9 @@ class DynamicSegment {
|
||||
}
|
||||
|
||||
generate(params:StringMap) {
|
||||
if (!StringMapWrapper.contains(params, this.name)) {
|
||||
throw new BaseException(`Route generator for '${this.name}' was not included in parameters passed.`)
|
||||
}
|
||||
return StringMapWrapper.get(params, this.name);
|
||||
}
|
||||
}
|
||||
@ -118,6 +121,7 @@ export class PathRecognizer {
|
||||
}
|
||||
|
||||
generate(params:StringMap):string {
|
||||
return ListWrapper.join(ListWrapper.map(this.segments, (segment) => '/' + segment.generate(params)), '');
|
||||
return ListWrapper.join(ListWrapper.map(this.segments, (segment) =>
|
||||
'/' + segment.generate(params)), '');
|
||||
}
|
||||
}
|
||||
|
20
modules/angular2/src/router/router_link.js
vendored
20
modules/angular2/src/router/router_link.js
vendored
@ -1,5 +1,6 @@
|
||||
import {Directive} from 'angular2/src/core/annotations_impl/annotations';
|
||||
import {Directive, onAllChangesDone} from 'angular2/src/core/annotations_impl/annotations';
|
||||
import {ElementRef} from 'angular2/core';
|
||||
import {StringMap, StringMapWrapper} from 'angular2/src/facade/collection';
|
||||
|
||||
import {isPresent} from 'angular2/src/facade/lang';
|
||||
import {DOM} from 'angular2/src/dom/dom_adapter';
|
||||
@ -32,33 +33,40 @@ import {Router} from './router';
|
||||
properties: {
|
||||
'route': 'routerLink',
|
||||
'params': 'routerParams'
|
||||
}
|
||||
},
|
||||
lifecycle: [onAllChangesDone]
|
||||
})
|
||||
export class RouterLink {
|
||||
_domEl;
|
||||
_route:string;
|
||||
_params:any;
|
||||
_router:Router;
|
||||
//TODO: handle click events
|
||||
_href:string;
|
||||
|
||||
constructor(elementRef:ElementRef, router:Router) {
|
||||
this._domEl = elementRef.domElement;
|
||||
this._router = router;
|
||||
this._params = StringMapWrapper.create();
|
||||
DOM.on(this._domEl, 'click', (evt) => {
|
||||
evt.preventDefault();
|
||||
this._router.navigate(this._href);
|
||||
});
|
||||
}
|
||||
|
||||
set route(changes) {
|
||||
this._route = changes;
|
||||
this.updateHref();
|
||||
}
|
||||
|
||||
set params(changes) {
|
||||
this._params = changes;
|
||||
this.updateHref();
|
||||
}
|
||||
|
||||
updateHref() {
|
||||
onAllChangesDone() {
|
||||
if (isPresent(this._route) && isPresent(this._params)) {
|
||||
var newHref = this._router.generate(this._route, this._params);
|
||||
this._href = newHref;
|
||||
// Keeping the link on the element to support contextual menu `copy link`
|
||||
// and other in-browser affordances.
|
||||
DOM.setAttribute(this._domEl, 'href', newHref);
|
||||
}
|
||||
}
|
||||
|
2
modules/angular2/src/router/router_outlet.js
vendored
2
modules/angular2/src/router/router_outlet.js
vendored
@ -18,7 +18,7 @@ export class RouterOutlet {
|
||||
_router:routerMod.Router;
|
||||
_viewContainer:ViewContainerRef;
|
||||
|
||||
constructor(viewContainer:ViewContainerRef, compiler:Compiler, router:routerMod.Router, injector:Injector, @Attribute('name') nameAttr) {
|
||||
constructor(viewContainer:ViewContainerRef, compiler:Compiler, router:routerMod.Router, injector:Injector, @Attribute('name') nameAttr:String) {
|
||||
if (isBlank(nameAttr)) {
|
||||
nameAttr = 'default';
|
||||
}
|
||||
|
Reference in New Issue
Block a user