Angularjs Angular js控制器设计将http post响应的数据传递给另一个控制器

Angularjs Angular js控制器设计将http post响应的数据传递给另一个控制器,angularjs,angularjs-controller,Angularjs,Angularjs Controller,我对Angularjs是新手。我有一个关于控制器设计和数据共享的问题 我有第一个部分upload.html,它使用UploadCntrl将word文档上传到服务器,使用$http.post。服务器调用封装在uploadService中。在服务器端,我分析文档中单词列表的出现频率,并将该信息作为对$http.post的响应返回 回到UploadCntrl,成功后,我想引导/显示第二个partial report.html,它使用AnalyseCntrl以表格形式显示分析响应($http.post)

我对Angularjs是新手。我有一个关于控制器设计和数据共享的问题

我有第一个部分
upload.html
,它使用
UploadCntrl
将word文档上传到服务器,使用
$http.post
。服务器调用封装在
uploadService
中。在服务器端,我分析文档中单词列表的出现频率,并将该信息作为对
$http.post
的响应返回

回到
UploadCntrl
,成功后,我想引导/显示第二个partial report.html,它使用
AnalyseCntrl
以表格形式显示分析响应(
$http.post

如何在
UploadCntrl
中与
AnalyseCntrl
共享分析响应


这是设计控制器的正确方法吗?

方法是创建一个服务并将其注入两个控制器

app.service("sharedService", function () {
    return {
        sharedObject: {data: null}
    };
});
现在在每个控制器中,如下所示:

app.controller("myController", ["$scope", "$http", "$filter", "storageService",   function($scope, $http, $filter, storageService) {

    // We don't need to put it on the $scope but it makes it available to the view
    $scope.shared = storageService.sharedObject;
    ...
    // After the following line, the other controller will have access to 
    // storageService.sharedObject.myThing as well. Easy!
    $scope.shared.myThing = "your thing";
    ...
$scope.$on('response-recieved', function() {
    var myResponse = MyService.getResponse();
}

实现这一点的方法是创建一个服务并将其注入两个控制器

app.service("sharedService", function () {
    return {
        sharedObject: {data: null}
    };
});
现在在每个控制器中,如下所示:

app.controller("myController", ["$scope", "$http", "$filter", "storageService",   function($scope, $http, $filter, storageService) {

    // We don't need to put it on the $scope but it makes it available to the view
    $scope.shared = storageService.sharedObject;
    ...
    // After the following line, the other controller will have access to 
    // storageService.sharedObject.myThing as well. Easy!
    $scope.shared.myThing = "your thing";
    ...
$scope.$on('response-recieved', function() {
    var myResponse = MyService.getResponse();
}

在AngularJS中共享数据最好通过或完成。另一个选项是将响应存储在浏览器的
本地存储器中,然后在
分析中心中访问它

因此,类似于上载Cntrl中的内容:

anaylseAPI.sendToAnalyse(object).then( function (response) {
 localStorage.setItem("howMany", response.amount);
});
然后访问
AnaylseCntrl
中的
localStorage

另一个解决方案是使用服务。

它们的行为类似于单例,因此您可以将它们用作数据存储

angular
  .module( 'app', [])
  .service('DataStore', DataStore);

function DataStore() {

  this.amount = 0;

  return function(amountOf) {
    this.amount = amountOf;
  };
}

现在
在两个控制器中插入服务,并保存/获取其中的数据。

在AngularJS中共享数据最好通过或完成。另一个选项是将响应存储在浏览器的
本地存储器中,然后在
分析中心中访问它

因此,类似于上载Cntrl中的内容:

anaylseAPI.sendToAnalyse(object).then( function (response) {
 localStorage.setItem("howMany", response.amount);
});
然后访问
AnaylseCntrl
中的
localStorage

另一个解决方案是使用服务。

它们的行为类似于单例,因此您可以将它们用作数据存储

angular
  .module( 'app', [])
  .service('DataStore', DataStore);

function DataStore() {

  this.amount = 0;

  return function(amountOf) {
    this.amount = amountOf;
  };
}

现在
在两个控制器中插入服务,并保存/获取其中的数据。

您可以编写一个angularjs服务,负责发送post请求。在这个服务中,您可以让一个变量存储您的响应,并提供一个getter方法来检索这个变量的值

此外,您可能希望提供一个函数,将您的文件作为参数,并发出
$http
请求。一旦得到服务器的响应,就将响应值分配给变量并发送广播

广播可以是这样的

$rootScope.$broadcast('response-recieved');
然后,您需要做的是将此服务注入两个控制器中,侦听广播事件,然后调用服务的get方法来检索响应

您可以这样做:

app.controller("myController", ["$scope", "$http", "$filter", "storageService",   function($scope, $http, $filter, storageService) {

    // We don't need to put it on the $scope but it makes it available to the view
    $scope.shared = storageService.sharedObject;
    ...
    // After the following line, the other controller will have access to 
    // storageService.sharedObject.myThing as well. Easy!
    $scope.shared.myThing = "your thing";
    ...
$scope.$on('response-recieved', function() {
    var myResponse = MyService.getResponse();
}

您可以找到如何使用服务的示例。如果这对您有帮助,请告诉我。

您可以编写一个angularjs服务,负责发送您的post请求。在这个服务中,您可以让一个变量存储您的响应,并提供一个getter方法来检索这个变量的值

此外,您可能希望提供一个函数,将您的文件作为参数,并发出
$http
请求。一旦得到服务器的响应,就将响应值分配给变量并发送广播

广播可以是这样的

$rootScope.$broadcast('response-recieved');
然后,您需要做的是将此服务注入两个控制器中,侦听广播事件,然后调用服务的get方法来检索响应

您可以这样做:

app.controller("myController", ["$scope", "$http", "$filter", "storageService",   function($scope, $http, $filter, storageService) {

    // We don't need to put it on the $scope but it makes it available to the view
    $scope.shared = storageService.sharedObject;
    ...
    // After the following line, the other controller will have access to 
    // storageService.sharedObject.myThing as well. Easy!
    $scope.shared.myThing = "your thing";
    ...
$scope.$on('response-recieved', function() {
    var myResponse = MyService.getResponse();
}

您可以找到如何使用服务的示例。如果这对您有帮助,请告诉我。

感谢所有的答案。谢谢。服务是单例的,因此在多个请求/多用户/多浏览器情况下,当服务与多个控制器共享以存储post请求的响应时,是否会导致任何问题?感谢所有答案。谢谢。服务是单例的,所以在多个请求/多用户/多浏览器的情况下,当服务与多个控制器共享以存储post请求的响应时,会导致任何问题吗?