2016-04-20 3 views
1

У меня есть AngularJS сервис и контролер код.Как позвонить услуге 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
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(); 
    }); 
+0

Я получаю неперехваченный ReferenceError: ошибка AttendanceService не определена – akash

+0

вам нужно вводить свой сервис «» AttendanceService с кавычками. как «AttendanceService» или «AttendanceService» в ваш контроллер ......... –

+0

Я уже сделал, что – akash