How can I setup a global function that can be accessed throughout all views?
In app.component.ts I added a simple method
openShare() {console.log("button clicked")}
and then in my nav I have
<button ion-button right (click)="openShare()">
<ion-icon name="md-share"></ion-icon>
</button>
When I try to access this from any page I get the following.
self.context.openShare is not a function
It does however execute fine if I put it directly in the constructor (e.g this.openShare(); ), but when calling from any page using a (click) function it just doesn't work.
Is app.component.ts not global? I thought this is where I would place it, but maybe I am missing something.
Basically its a function for a simple button on the nav, I need it to be global though since its used on every page.
Any help would be appreciated, still figuring Ionic 2 out.
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();
}
}
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);
};
}
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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With