fix(RouterLink): do not prevent default behavior if target set on anchor element

If the anchor element on which the "router-link" directive is present has a target
attribute other than "_self," the handler will not prevent default behavior of
the browser.

Closes #4233
Closes #5082
This commit is contained in:
Jeff Cross
2015-11-02 15:11:57 -08:00
parent a9b1270a5a
commit a69e7fe297
4 changed files with 71 additions and 7 deletions

View File

@ -1,5 +1,5 @@
import {Directive} from '../core/metadata';
import {StringMapWrapper} from 'angular2/src/core/facade/collection';
import {isString} from 'angular2/src/core/facade/lang';
import {Router} from './router';
import {Location} from './location';
@ -36,7 +36,7 @@ import {Instruction, stringifyInstruction} from './instruction';
*/
@Directive({
selector: '[router-link]',
inputs: ['routeParams: routerLink'],
inputs: ['routeParams: routerLink', 'target: target'],
host: {
'(click)': 'onClick()',
'[attr.href]': 'visibleHref',
@ -48,6 +48,7 @@ export class RouterLink {
// the url displayed on the anchor element.
visibleHref: string;
target: string;
// the instruction passed to the router to navigate
private _navigationInstruction: Instruction;
@ -65,7 +66,11 @@ export class RouterLink {
}
onClick(): boolean {
this._router.navigateByInstruction(this._navigationInstruction);
return false;
// If no target, or if target is _self, prevent default browser behavior
if (!isString(this.target) || this.target == '_self') {
this._router.navigateByInstruction(this._navigationInstruction);
return false;
}
return true;
}
}