Reputation: 157
I want to send object to angularjs Controller using MVC Controller Action is it possible?
suppose
public ActionResult Dashboard()
{
return View();
}
I want to pass object to app.js how to do this
Upvotes: 1
Views: 1169
Reputation: 888
Your question is a bit vague , you need to be more specific on what exactly you are trying to do.
Generally , this his how you would get data in Angular from the MVC application.
In Case of MVC/WebAPI , you should use actions to return JSON result back to the angular service which can then be processed by angular. Example below :
app.factory('myService', function($http) {
var myService = {
GetData: function() {
// $http returns a promise, which has a then function, which also returns a promise
var promise = $http.get('<ActionURL>').then(function (response) {
// The then function here is an opportunity to modify the response
console.log(response);
// The return value gets picked up by the then in the controller.
return response.data;
});
// Return the promise to the controller
return promise;
}
};
return myService;
});
app.controller('MainCtrl', function( myService,$scope) {
// Call the async method and then do stuff with what is returned inside our own then function
myService.GetData().then(function(d) {
$scope.data = d;
});
});
After this services is called from the MainCtrl , angular will have the data from the MVC action available in its $scope.data variable.
Upvotes: 2