akash
akash

Reputation: 173

How to call angularJs service on button click?

I have this AngularJS service and controller code.

 angular.module('myModule', []).service("AttendanceService", function ($http) {
        this.getdata = function () {

            return $http({
                method: "post",
                url: "Dashboard/GetAttendanceReport",
                params: [{ EmpID: $("#nameofEmp").val(), YearID: $("#YearIn").val() }],
                dataType: "json"
            });
        };
}).controller('myController', function ($scope, AttendanceService) {

        GetAlldata();
        function GetAlldata()
           {
            var getAttendanceData = AttendanceService.getdata();  
           }
            })

What I want is simply to call the above service i.e

return $http({
                method: "post",
                url: "Dashboard/GetAttendanceReport",
                params: [{ EmpID: $("#nameofEmp").val(), YearID: $("#YearIn").val() }],
                dataType: "json"
            });

on button click

   $("#btnLoad").click(function (event) {

    });

Is it possible? I am very new to this...

Upvotes: 1

Views: 1463

Answers (1)

Bimal Das
Bimal Das

Reputation: 2002

You need to change your factory like this :

    angular.module('myModule', []).service("AttendanceService", function ($http) {            
            var fac = {};
            fac.getdata = function () {

                return $http({
                    method: "post",
                    url: "Dashboard/GetAttendanceReport",
                    params: [{ EmpID: $("#nameofEmp").val(), YearID: $("#YearIn").val() }],
                    dataType: "json"
                });
            };

return fac;
})



$("#btnLoad").click(function (event) {
         AttendanceService.getdata();
    });

Upvotes: 1

Related Questions