1

I'm learning AngularJS and I've set it up with an mvc application. I'm trying to convert a small piece of code that was written before in JQuery to AngularJS but can't figure out how to get it working. The problem is that I don't know how to call a codebehind method in my controller with AngularJS ?

This is how it is working now in JQuery:

//JQuery calling code behind
$(document).on("click", ".open-AppDescriptionDialog", function () {
    var title = "Title";
    var state = "active";

    //call method
    $.post('<%= Url.Action("StatusInfoString") %>', { header: title, status: state }, ParseResult);
});



//method in controller
[HttpPost]
public ActionResult StatusInfoString(string path, string status)
{
     ServiceClient serviceClient = new ServiceClient();
     var data = serviceClient.GetResults();

     return Content(data);
 }

Anyone an idea how this is done ?

1
  • look at $http service in angularjs Commented Aug 5, 2013 at 8:21

1 Answer 1

2

In angular their are different way to achieve this and angular has module for the same

Below is the list

http://docs.angularjs.org/api/ngResource.$resource

http://docs.angularjs.org/api/ng.$http

http://docs.angularjs.org/api/ng.$httpBackend

You need to inject this module from above, generally people write service for this using factory mehthod below is the example:

app.factory('myService', function($http) {
   return {
     getList:function(params){
          var promise= $http({url: 'ServerURL',method: "POST", params: params}).then(function(response,status){

            return response.data;
            });
          // Return the promise to the controller
          return promise; 
        }
   }
});

app.controller('MainCtrl', function($scope, myService) {
  myService.getList(function(data) {
     $scope.foo = data;
  });
});
Sign up to request clarification or add additional context in comments.

3 Comments

I'm sorry but I'm new to AngularJS. How would I go and actually implement this? Can you link to some small example?
Hi, I'm getting 'myService is undefined'. How do I create 'myService' ?
Hi check the working example @ plnkr.co/edit/3Eoq0zmv5NnOpHvyccaS?p=preview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.