I am new to Typescript and Angular 2. I tried to look for an answer in the web but it seems they don't work for me.
Say I have an app.component as shown below:
export class AppComponent implements OnInit {
constructor(public _testService: TestService) { }
listForCart = this._testService.getListForCart();
cartCount = this.listForCart.length;
cartPayableAmount = 0;
ngOnInit() {
this.computeTotal();
}
TestingFunction(){
console.log("Got here");
}
}
Now I want to access the TestingFunction in my app.component in other class as shown below:
export class BuyTestComponent {
constructor(private _testService: TestService) {
}
public pageTitle: string = "ALL PRACTICE TEST SERIES";
TestHere() {
// should call TestingFunction() here.....
}
}
How to do that? Please help
The question does not ask component interaction, it asks for calling a component method from a service. This simply can be achieved by injecting service to the component. Then define a method inside the service which takes a function as parameter.
To call another components function in Angular, we can inject the component we want to call the function on as a dependency. export class OneComponent implements OnInit, AfterViewInit { //... ngOnInit() {} public testCall() { alert("I am here.."); } //... }
You can call child component in the parent component using the selector as directive on HTML element. Ways to call component by class and directive works same as selector as tag. It just depends on the use case required. To use selector as directive, change is required in the selector of the child component.
If you need access to a function from several places, consider putting it in a service as @tibbus mentioned.
utility.service.ts
@Injectable()
export class UtilityService{
TestingFunction(){}
}
Next, make sure the service is listed in the providers
array of your main module:
app.module.ts
// https://angular.io/docs/ts/latest/guide/ngmodule.html#!#ngmodule-properties
@NgModule({
imports: [ BrowserModule],
declarations: [ AppComponent, BuyTestComponent ],
providers: [ UtilityService ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
You can then inject that service in any component that needs access to the function
buy-test.component.ts
@Component(...)
export class BuyTestComponent {
//inject service into the component
constructor(private util:UtilityService){}
TestHere() {
//access service function
this.util.TestingFunction();
}
}
Assuming your class AppCompenent is saved as app.component.ts Then in your BuyTestComponent class, you need to first import it AppComponent like below
import {AppComponent} from '../app.component';
Assuming both files are saved in the same folder.
Instantiate it in your constructor like below
constructor(public myapp: AppComponent){}
then call it in your BuyTestComponent like below
this.myapp.testingFunction();
lastly, you need to register it as a provider in your app.module.ts
providers: [
AppComponent,
]
Angular2 has 2 ways to communicate between 2 components :
Both ways are detailed in this article from Angular2 docs : https://angular.io/docs/ts/latest/cookbook/component-communication.html
I think the simplest way to achieve this is, basically create a static property in the app component that is populated when onInit
In app.component
export class AppComponent implements OnInit {
static myapp;
ngOnInit() {
AppComponent.myapp = this;
}
}
then in your component
import { AppComponent } from '../../app.component';
export class UsersComponent implements OnInit {
ngOnInit() {
console.log(AppComponent.myapp);
}
}
You can use Events like:
app.component.ts
import { Events } from 'ionic-angular';
constructor(public events: Events) {
events.subscribe('myEvent', () => {
this.myMethod();
});
}
myMethod() {
console.log("my method was activated");
}
anotherclass
import { Events } from 'ionic-angular';
constructor(public events: Events) {
}
callEvent() {
this.events.publish('myEvent');
}
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