Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用MVC控制器动作将对象传递给angularjs控制器_Angularjs_Model View Controller - Fatal编程技术网

如何使用MVC控制器动作将对象传递给angularjs控制器

如何使用MVC控制器动作将对象传递给angularjs控制器,angularjs,model-view-controller,Angularjs,Model View Controller,我想使用MVC控制器操作将对象发送到angularjs控制器,有可能吗 假设 public ActionResult Dashboard() { return View(); } 我想把object传递给app.js如何做你的问题有点模糊,你需要更具体地说明你到底想做什么 通常,这是如何从MVC应用程序获取角度数据的 对于MVC/WebAPI,您应该使用操作将JSON结果返回给angular服务,然后angular可以对其进行处理。 示例如下: app.f

我想使用MVC控制器操作将对象发送到angularjs控制器,有可能吗

假设

 public ActionResult Dashboard()
    {
        return View();
    }

我想把object传递给app.js如何做你的问题有点模糊,你需要更具体地说明你到底想做什么

通常,这是如何从MVC应用程序获取角度数据的

对于MVC/WebAPI,您应该使用操作将JSON结果返回给angular服务,然后angular可以对其进行处理。 示例如下:

  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;
  });
});
app.factory('myService',函数($http){
var myService={
GetData:function(){
//$http返回一个承诺,它有一个then函数,该函数也返回一个承诺
var promise=$http.get(“”).then(函数(响应){
//此处的then函数是修改响应的机会
控制台日志(响应);
//返回值由控制器中的then获取。
返回响应数据;
});
//将承诺返还给控制者
回报承诺;
}
};
返回myService;
});
app.controller('MainCtrl',函数(myService,$scope){
//调用async方法,然后处理我们自己的then函数中返回的内容
myService.GetData().then(函数(d){
$scope.data=d;
});
});

从MainCtrl调用此服务后,angular将在其$scope.data变量中提供来自MVC操作的数据。

更具体一些。也发布代码。通过
MVC控制器操作
您是指来自
Spring
Controller?否来自Asp.net MVC控制器操作表示返回视图()我想将数据传递给angularjs控制器我想知道这是否可能这是首选方法。还可以将一个模型反序列化为json,这样可以节省到server@JeffDunlop:你是什么意思?从评论中看不出你的意思。有样品吗?