简体   繁体   English

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

[英]How to call angularJs service on button click?

I have this AngularJS service and controller code.我有这个 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();  
           }
            })

What I want is simply to call the above service ie我想要的只是调用上述服务即

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...我对此很陌生......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM