简体   繁体   English

在两个控制器angularjs之间共享数据

[英]Sharing data between two controllers angularjs

I seem to be facing a problem with sharing data between controllers in angularjs I have two HTML files, one controller for each of them, a service to share Information and a regular app file for routing. 我似乎在angularjs的控制器之间共享数据时遇到问题,我有两个HTML文件,每个控制器一个控制器,一个用于共享信息的服务和一个用于路由的常规应用程序文件。

Here's the first file 这是第一个文件

<div class="container">
<button ng-click="broadcastData()"> Send  </button> </div>

Here's the corresponding Controller for it: 这是对应的控制器:

angular.module('myApp').controller('sendInfoController',
 ['$scope','$rootScope','$location'
  function ($scope,$rootScope, $location) 
{
    $scope.broadcastData=function()
    {
        shareInfoService.sendData({info: "Okay"});
        $location.path('/infoView');
    }
}]);

Here's the second HTML File: (infoView.html) 这是第二个HTML文件:(infoView.html)

<div>
 {{data}}
</div>

Here's the corresponding controller for it: 这是它的相应控制器:

angular.module('myApp').controller('infoController',
 ['$scope','$rootScope',
function ($scope,$rootScope) 
 {
    $scope.data="hello";
    $rootScope.$on('sendTheData', function(event,args)
    {
            console.log(args);
            $scope.data=args.info;
    });
    console.log($scope.data);
}]);

Here's the service to share information: 这是共享信息的服务:

angular.module('prkApp').factory('shareInfoService',
  ['$rootScope',
 function ($rootScope) {

//Return the particular function
return ({
  sendData: sendData
});

function sendData(data)
{
    $rootScope.$broadcast('sendTheData', data);
};
}]);

When I click on the button in the first HTML File, the location gets changed to infoView and the shareInfoService.broadcastData function gets called. 当我单击第一个HTML文件中的按钮时,该位置将更改为infoView并调用shareInfoService.broadcastData函数。

It redirects to the second HTML File. 它将重定向到第二个HTML文件。 However, the information that is displayed on this page is "hello" and not "Okay". 但是,此页面上显示的信息是“你好”,而不是“好”。

The web console logs shows {info: "Okay"} first and "hello" immediately after that. Web控制台日志首先显示{info:“ Okay”},其后立即显示“ hello”。

How can it be rectified so as to show the data that is sent by the previous controller? 如何纠正它以显示前一个控制器发送的数据?

You are sending out the broadcast before you change the location so the listener in the infoController will not be loaded when the event is broadcasted. 更改位置之前,您正在发送广播,因此广播事件时将不会加载infoController的侦听器。 Instead of broadcasting, you can store the value in the service and then have infoController retrieve it when it loads. 您可以将值存储在服务中,而不是广播,然后让infoController在加载时检索它。

Service 服务

angular.module('prkApp').factory('shareInfoService', ['$rootScope', function($rootScope) {
    var data;

    //Return the particular function
    return ({
      sendData: sendData,
      getData: getData
    });

    function sendData(new_data) {
      data = new_data;
    };

    function getData() {
      return data;
    }
  }
]);

Controller 控制者

angular.module('myApp').controller('infoController', ['$scope', '$rootScope', function($scope, $rootScope) {
    $scope.data = "hello";
    console.log($scope.data);
    $scope.data = shareInfoService.getData();
    console.log($scope.data);
  }
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM