global function in Ionic 2 / Angular 2
You can easily do that using Providers
.When you need to use that functionality (or provider), you just need to inject
it into your related component.That is it.
Method 1:
You can create a Provider using CLI.
> ionic g provider YourProvider
your-provider.ts
import { Injectable } from '@angular/core';
@Injectable()
export class YourProvider {
constructor() {
}
openShare() {console.log("button clicked")}
}
app.module.ts
import { YourProvider } from "../pages/path";
@NgModule({
declarations: [
MyApp,
],
imports: [
IonicModule.forRoot(MyApp),
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
],
providers: [{ provide: ErrorHandler, useClass: IonicErrorHandler },YourProvider ]
})
export class AppModule { }
your-view.ts
import { YourProvider } from '../../providers/your-provider';
export class YourViewPage{
constructor(public yourProvider : YourProvider) {
}
openShare(){
this.yourProvider.openShare();
}
Method 2: Creat an abstract
base class.
my-base-class.ts
export abstract class MyBaseClass {
constructor() {
}
protected openShare():void {
console.log("button clicked");
}
}
my-view.ts
export class MyViewPage extends MyBaseClass {
constructor()
{
super();
}
openShare(){
super.openShare();
}
}
Just like @Sampath mentioned, one way would be to use a custom provider. But since you method is just a simple one, I think you can use Events instead. It'd be like this:
In your app.component.ts
file, subscribe to the new event:
import { Events } from 'ionic-angular';
constructor(public events: Events, ...) {
// ...
events.subscribe('social:share', () => {
// your code...
console.log("button clicked");
});
}
And then in any other page, just publish the event to execute that logic:
function anotherPageMethod() {
this.events.publish('social:share');
}
You can use Directive
.
Try as follows.
Put the following code in separate directive file. (social-sharing.directive.ts);
import { Directive, HostListener } from '@angular/core';
@Directive({
selector: '[socialSharing]'
})
export class SocialSharing {
constructor() { }
@HostListener('click') onClick() {
// Your click functionality
}
}
Import it into app.module.ts
and than add to declarations
.
In HTML, just add attribute
to any element which is the selector
in your directive file.
<button ion-button right socialSharing>
<ion-icon name="md-share"></ion-icon>
</button>
Additional Info:
Passing values from component to directive.
home.html
<button ion-button right [socialSharing]="property">
<ion-icon name="md-share"></ion-icon>
</button>
home.component.ts
export class HomePage {
property: string = 'some url';
constructor() {}
}
social-sharing.directive.ts
Import Input
along with others from @angular/core
.
import { Directive, Input, HostListener } from '@angular/core';
@Directive({
selector: '[socialSharing]'
})
export class SocialSharing {
@Input('socialSharing') str: string;
constructor() {}
@HostListener('click') onClick() {
console.log(this.str);
}
};
ngAfterInit() {
console.log(this.str);
};
}
Using Element in Directive:
social-sharing.directive.ts
Import ElementRef
along with others from @angular/core
import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[socialSharing]'
})
export class SocialSharing {
// Add ElementRef to constructor
constructor(el: ElementRef) {};
ngOnInit() {
let elRef = this.el.nativeElement;
console.log(elRef.innerHTML);
};
}