From c8d9685bc9dca759799d0305e0de7b53451ed09d Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Wed, 5 Dec 2018 21:18:03 +0100 Subject: [PATCH] fix(angular): routerLink updates href --- .../directives/navigation/href-delegate.ts | 30 ++++++++----------- core/src/components/item/item.tsx | 2 +- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/angular/src/directives/navigation/href-delegate.ts b/angular/src/directives/navigation/href-delegate.ts index abeaa2d78c..ab83926fd2 100644 --- a/angular/src/directives/navigation/href-delegate.ts +++ b/angular/src/directives/navigation/href-delegate.ts @@ -1,12 +1,12 @@ import { Directive, ElementRef, HostListener, Input, Optional } from '@angular/core'; import { NavController, NavDirection } from '../../providers/nav-controller'; -import { NavigationEnd, Router, RouterLink } from '@angular/router'; +import { Router, RouterLink } from '@angular/router'; import { LocationStrategy } from '@angular/common'; import { Subscription } from 'rxjs'; @Directive({ - selector: '[routerLink]' + selector: '[routerLink]', }) export class HrefDelegate { @@ -20,22 +20,10 @@ export class HrefDelegate { private navCtrl: NavController, private elementRef: ElementRef, @Optional() private routerLink?: RouterLink, - ) { - if (this.routerLink) { - this.subscription = router.events.subscribe(s => { - if (s instanceof NavigationEnd) { - this.updateTargetUrlAndHref(); - } - }); - } - } + ) { } - updateTargetUrlAndHref() { - if (this.routerLink) { - const href = this.locationStrategy.prepareExternalUrl(this.router.serializeUrl(this.routerLink.urlTree)); - console.log(href); - this.elementRef.nativeElement.href = href; - } + ngOnInit() { + this.updateTargetUrlAndHref(); } ngOnChanges(): any { @@ -48,6 +36,14 @@ export class HrefDelegate { } } + private updateTargetUrlAndHref() { + if (this.routerLink) { + const href = this.locationStrategy.prepareExternalUrl(this.router.serializeUrl(this.routerLink.urlTree)); + console.log(href); + this.elementRef.nativeElement.href = href; + } + } + @HostListener('click', ['$event']) onClick(ev: UIEvent) { if (this.routerDirection) { diff --git a/core/src/components/item/item.tsx b/core/src/components/item/item.tsx index 99e6b1ecd7..37672908d6 100644 --- a/core/src/components/item/item.tsx +++ b/core/src/components/item/item.tsx @@ -153,7 +153,7 @@ export class Item implements ComponentInterface { openURL(win, href, ev, routerDirection)} + onClick={(ev: Event) => openURL(win, href, ev, routerDirection)} >