is it possible to call a function from service in onclick event in angular 4

angular 4 click event example
event binding in angular 7
angular 6 href=#'' (click)
angular 7 click event on div
angular trigger click
angular 6 get clicked element
call angular function from html onclick
angular 8 call function from html

I am a new bee in angular 4. I have written a method getDataMethod() in my service 'data.service.ts'.

Now I want to call this function when some one click on a submit button from my html page.

how can we call this function? please help me.

i have created an instance of my service in constructor of my component like below

cnstructor(private dataservice: DataService){ = this.dataservice.getDataMethod();

You need to create an instance of the service inside the constructor of the component, then refer the service and call the method.

import { DataService } from './data.service';

export Class ComponentA { 
 constructor(public dataService: DataService) { } 

Call a Function on click Event in Angular 9/8, angular 9/8 button click call function, angular 9/8 button click event example, angular 9/8 click event on button, click id: 4,. title: 'How to Create New Component in Angular 8?' How to Create Service in Angular 8 using cli? In this chapter, we will discuss how Event Binding works in Angular 4. When a user interacts with an application in the form of a keyboard movement, a mouse click, or a mouseover, it generates an event. These events need to be handled to perform some kind of action. This is where event binding comes into picture.

You need to provide your service to a parent module or component itself (you can take one other approach in angular v6, take a look at official docs) and then inject it into your component, then you can use it on click or submit event.

Component(.ts) file:

export class TestComponent {
    constructor(public testService: TestService) {

Template (.html) file:

<button (click)="testService.getDataService()">Button</button>

Although it's better if you call the service method from a method declared inside the component.

How to Programmatically Trigger 'Click' Event in Angular 4 & 5, How to Programmatically Trigger 'Click' Event in Angular 4 & 5 and as much as possible I like to keep my code free from imports that I can't reuse. The method onFileChange($event) displays the file name of the file being that will store the data on your server or call a third-party service like AWS S3. Call Multiple JavaScript Functions In Onclick Event Complete steps is described in this video regarding How To Call Multiple JavaScript Functions In Onclick Event If you got any clarification on

As @Sajeetharan already mentioned, you will have to call the service from the component.

Check out this stackblitz POC which also shows how to import the created service in your module before you can inject it in the component.


import { Injectable } from '@angular/core';

export class Service {

  public getData(): string {
    return "Data From Service";


import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { Service } from './service'

  imports:      [ BrowserModule, FormsModule ],
  declarations: [ AppComponent, HelloComponent ],
  providers: [Service],
  bootstrap:    [ AppComponent ]
export class AppModule { }


import { Component } from '@angular/core';
import { Service } from './service'

    selector: 'my-app',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
export class AppComponent {
    name = 'Angular 6';

    public data: string;

    constructor(private _Service: Service) {


    public getData(): void { = this._Service.getData();



<hello name="{{ name }}"></hello>

<button (click)="getData()">get data</button>

<p> data from service - {{data}} </p>

Simple Way To Execute A Function In A Component From Another , In Angular, a component can emit an event using @Output and EventEmitter. We can use various options to execute functions from another <p>; First Component; </p>; <div>; <input type="button" value="Call Function" (click)="​firstFunction()">; </div> We can create this service using Angular CLI. When a user interacts with your app, it's sometimes necessary to know when this happens. A click, hover, or a keyboard action are all events that you can use to call component logic within Angular. That's what Angular event binding is all about. It's one-way data binding, in that it sends information from the view to the component class.

Event Binding in Angular, Let's consider an example where we are binding an onClick() event the button's click event and calls the component's onClick() method. File Name: example.component.ts. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import { Component } from We use cookies to make interactions with our websites and services  i have use angular 2.4 and i need to implement server side data table in my app so this purpose i have use this library but all functionality working except any angular event (ex. click) so any way to handle angular event in my app i nee

How to call ngOnInit() again in Angular 2?, in our method. In Chrome, just hit CTRL-SHIFT-I to see the console and the resulting console. is it possible to calling a function in < a > onclick event? You can execute any client-side script in an event like that. Unless you are trying to execute a PHP function? I would have to agree with the others tho it should only take you like a minute to test that yourself. Btw, I changed the thread title. Please try to make your titles

Angular 4 Event Binding, need to be handled to perform some kind of action. You can use Angular event bindings to respond to any DOM event. Many DOM events are triggered by user input. Binding to these events provides a way to get input from the user. To bind to a DOM event, surround the DOM event name in parentheses and assign a quoted template statement to it.

  • can't we directly call a method of service without creating instance in component??