我有这个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) {
});
有可能吗?我对此很新...
答案 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();
});