gmocamilotd
9/20/2017 - 9:47 PM

de la inyeccion

(angular)

// 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
@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();
    }
}

If you need access to a function from several places, consider putting it in a service as mentioned before

el ejemplo completo: