feat(router): make RouterLink accept single values
This commit is contained in:
parent
3aa2606ff1
commit
b625f2471a
|
@ -16,7 +16,7 @@ import {
|
|||
} from '@angular/core';
|
||||
import {RouterOutletMap, Router} from '../router';
|
||||
import {RouteSegment, UrlSegment, Tree} from '../segments';
|
||||
import {isString, isPresent} from '../facade/lang';
|
||||
import {isString, isArray, isPresent} from '../facade/lang';
|
||||
import {ObservableWrapper} from '../facade/async';
|
||||
|
||||
@Directive({selector: '[routerLink]'})
|
||||
|
@ -25,8 +25,8 @@ export class RouterLink implements OnDestroy {
|
|||
private _comands: any[] = [];
|
||||
private _subscription: any;
|
||||
|
||||
@HostBinding() private href: string;
|
||||
@HostBinding('class.router-link-active') private isActive: boolean = false;
|
||||
@HostBinding() href: string;
|
||||
@HostBinding('class.router-link-active') isActive: boolean = false;
|
||||
|
||||
constructor(@Optional() private _routeSegment: RouteSegment, private _router: Router) {
|
||||
this._subscription =
|
||||
|
@ -36,8 +36,12 @@ export class RouterLink implements OnDestroy {
|
|||
ngOnDestroy() { ObservableWrapper.dispose(this._subscription); }
|
||||
|
||||
@Input()
|
||||
set routerLink(data: any[]) {
|
||||
this._comands = data;
|
||||
set routerLink(data: any[]|any) {
|
||||
if (isArray(data)) {
|
||||
this._comands = <any[]>data;
|
||||
} else {
|
||||
this._comands = [data];
|
||||
}
|
||||
this._updateTargetUrlAndHref();
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
import {
|
||||
AsyncTestCompleter,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachProviders,
|
||||
it,
|
||||
xit
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {provide, Component, ComponentResolver} from '@angular/core';
|
||||
import {RouterLink} from '../src/directives/router_link';
|
||||
import {
|
||||
Router,
|
||||
RouterOutletMap,
|
||||
RouteSegment,
|
||||
Route,
|
||||
ROUTER_DIRECTIVES,
|
||||
Routes,
|
||||
RouterUrlSerializer,
|
||||
DefaultRouterUrlSerializer,
|
||||
OnActivate,
|
||||
CanDeactivate
|
||||
} from '@angular/router';
|
||||
import {Location} from '@angular/common';
|
||||
import {SpyLocation} from '@angular/common/testing';
|
||||
|
||||
export function main() {
|
||||
describe('RouterLink', () => {
|
||||
beforeEachProviders(() => [
|
||||
provide(RouterUrlSerializer, {useClass: DefaultRouterUrlSerializer}),
|
||||
RouterOutletMap,
|
||||
provide(Location, {useClass: SpyLocation}),
|
||||
provide(Router,
|
||||
{
|
||||
useFactory: (resolver, urlParser, outletMap, location) => new Router(
|
||||
"RootComponent", RootCmp, resolver, urlParser, outletMap, location),
|
||||
deps: [ComponentResolver, RouterUrlSerializer, RouterOutletMap, Location]
|
||||
})
|
||||
]);
|
||||
|
||||
describe("routerLink=", () => {
|
||||
it("should accept an array of commands", inject([Router], (router) => {
|
||||
let link = new RouterLink(null, router);
|
||||
link.routerLink = ['/one', 11];
|
||||
expect(link.href).toEqual("/one/11");
|
||||
}));
|
||||
|
||||
it("should accept a single command", inject([Router], (router) => {
|
||||
let link = new RouterLink(null, router);
|
||||
link.routerLink = '/one/11';
|
||||
expect(link.href).toEqual("/one/11");
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Component({template: ''})
|
||||
class RootCmp {
|
||||
}
|
Loading…
Reference in New Issue