如何在按钮点击时调用angularJs服务?

时间:2016-04-20 10:03:25

标签: javascript angularjs asp.net-mvc

我有这个AngularJS 服务控制器代码。

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

我想要的只是调用上述服务,即

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

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

    });

有可能吗?我对此很新...

1 个答案:

答案 0 :(得分:1)

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