AngularJS calls a web method

I am learning AngularJS and I installed it using the mvc application. I am trying to convert a small piece of code that was written earlier in jQuery into AngularJS, but cannot figure out how to make it work. The problem is that I do not know how to call the codebehind method in my controller using AngularJS?

Here's how it works 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);
 }

Does anyone know how to do this?

+1
source share
1 answer

In angular, this is a different way for them, and angular has a module for the same

Below is a list

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

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

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

, , , factory mehthod , :

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;
  });
});
+2

Source: https://habr.com/ru/post/1546874/


All Articles