From 09e6b7e4a2617aad333a75f01f2cb1b74dfe51bc Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Tue, 3 Apr 2018 18:51:32 +0200 Subject: [PATCH] feat(angular): href integration --- angular/src/directives/index.ts | 1 + .../directives/navigation/href-delegate.ts | 30 +++++++++++++++++++ angular/src/module.ts | 1 + 3 files changed, 32 insertions(+) create mode 100644 angular/src/directives/navigation/href-delegate.ts diff --git a/angular/src/directives/index.ts b/angular/src/directives/index.ts index ec9629a51a..6f4cc990c2 100644 --- a/angular/src/directives/index.ts +++ b/angular/src/directives/index.ts @@ -11,6 +11,7 @@ export { NavDelegate } from './navigation/nav-delegate'; export { TabDelegate } from './navigation/tab-delegate'; export { TabsDelegate } from './navigation/tabs-delegate'; export { IonRouterOutlet } from './navigation/ion-router-outlet'; +export { HrefDelegate } from './navigation/href-delegate'; export { Icon } from './icon'; export { VirtualScroll } from './virtual-scroll/virtual-scroll'; diff --git a/angular/src/directives/navigation/href-delegate.ts b/angular/src/directives/navigation/href-delegate.ts new file mode 100644 index 0000000000..308c7db56e --- /dev/null +++ b/angular/src/directives/navigation/href-delegate.ts @@ -0,0 +1,30 @@ +import { Directive, ElementRef, HostListener, Input, Optional } from '@angular/core'; +import { Router } from '@angular/router'; + +@Directive({ + selector: 'ion-anchor,ion-button,ion-item' +}) +export class HrefDelegate { + + @Input() + set href(value: string) { + this.elementRef.nativeElement.href = value; + } + get href() { + return this.elementRef.nativeElement.href; + } + + constructor( + @Optional() private router: Router, + private elementRef: ElementRef + ) {} + + @HostListener('click', ['$event']) + onClick(ev: Event) { + const url = this.href; + if (this.router && url && url[0] !== '#' && url.indexOf('://') === -1) { + ev.preventDefault(); + this.router.navigateByUrl(url); + } + } +} diff --git a/angular/src/module.ts b/angular/src/module.ts index a13b496c63..1712dfff8c 100644 --- a/angular/src/module.ts +++ b/angular/src/module.ts @@ -100,6 +100,7 @@ const DECLARATIONS = [ c.NavDelegate, c.TabDelegate, c.TabsDelegate, + c.HrefDelegate, // virtual scroll c.VirtualFooter,