如何从角度服务调用动作方法?

时间:2017-02-14 04:59:42

标签: asp.net-mvc

我试图在angular service.js文件中调用mvc控制器。

     public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public JsonResult GetAll()
        {
            using (EmployeeEntities datacontext = new EmployeeEntities())
            {
                var employeelist = datacontext.Employees.ToList();
                return Json(employeelist, JsonRequestBehavior.AllowGet);
            }
        }

} }

角度控制器:

app.controller("mycontroller", function ($scope, myservice) {
debugger;
GetAllEmployee();
$scope.divEmployee = false;

function GetAllEmployee() {
    debugger;

    var getData = myservice.getAllEmployees();
    getData.then(function (emp) {
        $scope.employees = emp.data;
    }),
    function(){
        alert('Error in getting data');
    }
}

}); Angular service.js

pp.service("myservice", ["$http", function ($http) {
this.getAllEmployees = function () {
    debugger;
    var response = $http({
        url: "/Home/GetAll",
        method: "GET",

        datatype:'JSON'
    })
        return response;
    });

因此,当我尝试运行时调用Url," Home / GetAll"未找到错误即将发生。 那么如何调用服务中的URL。

0 个答案:

没有答案