user6440175 user6440175 - 2 months ago 6
AngularJS Question

Angular get data from the server MVC

I am trying to get data from the server.Get Users function has the data and I am trying to pass it to the AdminController.js. What am I doing wrong?
I am getting this error: TypeError: Cannot read property 'post' of undefined

namespace VTS.Controllers
{
public class AdminController : Controller
{
public ActionResult Index()
{
return View();
}

[HttpPost]
public JsonResult GetUsers()
{
JavaScriptSerializer jss = new JavaScriptSerializer();
var jsonString = string.Empty;
TributaryEntities db = new TributaryEntities();
var listUsers = (from user in db.PSP_BatchOrderDriver select new { ID = user.ID, Fname = user.FirstName, LastName = user.LastName }).ToList();
string output = jss.Serialize(listUsers);
var test = Json(jss.Serialize(output));
return test;
}
}
}


AdminController.js file

'user strict';
myApp.controller('AdminController',

function AdminControllerl($timeout, $q, $log)
{
var self = this;

self.simulateQuery = false;
self.isDisabled = false;

self.employees = loadAll();

function loadAll($http)
{
var allEmployees = $http.post('Controllers/AdminController/GetUsers');
}
}

Answer

Try including '$http' in controller's arguments

'user strict';
myApp.controller('AdminController',

function AdminControllerl($timeout, $q, $log, $http) 
{
    var self = this;

    self.simulateQuery = false;
    self.isDisabled = false;

    self.employees = loadAll();

    function loadAll()
    {
       var allEmployees = $http.post('Controllers/AdminController/GetUsers');
    }
}