AngularJS 帮助使用 $broadcast 在控制器之间共享数据 [英] AngularJS help sharing data between controllers using $broadcast

查看:24
本文介绍了AngularJS 帮助使用 $broadcast 在控制器之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 $broadcast 和 $on,但我无法让数据在两个控制器之间移动.

在我的主网页上,我有一个按钮,每次点击时都会将 4 添加到 var:

<html xmlns="http://www.w3.org/1999/xhtml"><头><title></title><body ng-app="app"><div ng-controller="mainCtrl"><button ng-click="add(4)">4 </按钮>

<script src="../Scripts/angular.js"></script><script src="../assets/js/angular-grid.js"></script><script src="../assets/controller/gods_controller.js"></script><script src="../Scripts/angular-animate.js"></script></html>

在我的 mainCtrl 我有函数 add() 并使用 $broadcast 来广播我的值:

var module = angular.module("app", ["angularGrid", "ngAnimate"])module.controller("mainCtrl", function ($scope, $rootScope) {无功总计 = 0$scope.add = 函数 (x) {总计 += x;$rootScope.$broadcast('totalBroadcast', total)}});

然后我有第二个控制器用于我的弹出窗口,使用 $on 来接收广播:

module.controller('popUpCtrl', function ($scope) {$scope.$on('totalBroadcast', function(events, args){$scope.total = args;})})

然后我的弹出 HTML 使用第二个控制器和 $scope.total 作为表达式:

 <html xmlns="http://www.w3.org/1999/xhtml"><头><title></title><body ng-app="app"><div ng-controller="popUpCtrl"><p>总数为:{{total}}</p>

<script src="../Scripts/angular.js"></script><script src="../assets/js/angular-grid.js"></script><script src="../assets/controller/gods_controller.js"></script><script src="../Scripts/angular-animate.js"></script></html>

似乎没有数据传递给我的第二个控制器,表达式根本没有显示任何内容.

编辑

<小时>

我也尝试过使用服务,上面的 HTML 保持不变,但现在我添加了服务

module.factory('myService', function () {无功总计 = 0;var setTotal = 函数 (x) {总计 = x;}var getTotal = 函数 () {总回报;};返回 {setTotal:setTotal,getTotal:getTotal}});

我的主控制器和广告功能现在是:

 module.controller("mainCtrl", function ($scope, myService) {无功总计 = 0$scope.add = 函数 (x) {myService.setTotal(x)}});

我的 popupCtrl 控制器现在是这样的:

module.controller('popUpCtrl', function ($scope, myService) {$scope.total = myService.getTotal();})

我的弹出窗口中的

{{total}} 现在被表示为0",在我的服务中 var total 等于.因此,现在正在使用该服务传输数据,但是 setTotal() 方法现在没有按照 add() 函数的指示设置 var.我已将我的服务作为依赖项注入到两个控制器中.

解决方案

我确实找到了解决我最初问题的方法,那就是使用 localStorage.setItem("num", "num")将数据存储在浏览器中.似乎这是专门为在选项卡/窗口之间共享数据而开发的,因此可以通过 localStorage.getItem("num") 访问.

这回答了我原来的问题,但是我现在希望 total 能够动态更新.目前它仅在刷新时更新.任何提示将不胜感激.

<小时>

编辑(以上答案)

通过在我的第二个弹出窗口中使用此事件侦听器 -

window.addEventListener('storage', function (event) {$scope.total = parseInt(event.newValue);$scope.$apply();});

我能够访问存储并将其绑定到我的 HTML 中的表达式.请记住,此处需要调用 $scope.apply(),因为 total 超出了范围.

这里有更多细节 - https://developer.apple.com/library/safari/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/Name-ValueStorage/Name-ValueStorage.html

I've been trying to use $broadcast and $on but I just can't get the data to move between the two controllers.

On my main webpage i have a button which adds 4 to a var each time it's hit:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body ng-app="app">
    <div ng-controller="mainCtrl">

        <button ng-click="add(4)"> 4 </button>

    </div>
    <script src="../Scripts/angular.js"></script>
    <script src="../assets/js/angular-grid.js"></script>
    <script src="../assets/controller/gods_controller.js"></script>
    <script src="../Scripts/angular-animate.js"></script>
</body>
</html>

In my mainCtrl I have the function add() and use $broadcast to broadcast my value:

var module = angular.module("app", ["angularGrid", "ngAnimate"])

module.controller("mainCtrl", function ($scope, $rootScope) {

var total = 0

    $scope.add = function (x) {
        total += x;

        $rootScope.$broadcast('totalBroadcast', total)

    }

});

Then I have a second controller for my popup using $on to receive the broadcast:

module.controller('popUpCtrl', function ($scope) {


     $scope.$on('totalBroadcast', function(events, args){

  $scope.total = args;
  })

   })

Then my popup HTML uses the second controller and $scope.total as an expression:

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body ng-app="app">
    <div ng-controller="popUpCtrl">

       <p>The total is: {{total}}</p>

    </div>
    <script src="../Scripts/angular.js"></script>
    <script src="../assets/js/angular-grid.js"></script>
    <script src="../assets/controller/gods_controller.js"></script>
    <script src="../Scripts/angular-animate.js"></script>
</body>
</html>

No data seems to be passed to my second controller, the expression doesn't show anything at all.

EDIT


I have also tried using a service, HTML's above remain the same but now I've added a service

module.factory('myService', function () {

    var total = 0;

    var setTotal = function (x) {
        total = x;
    }

    var getTotal = function () {
        return total;
    };

    return {
        setTotal: setTotal,
        getTotal: getTotal
    }


    });

My main controller and ad function are now:

 module.controller("mainCtrl", function ($scope, myService) {

    var total = 0

    $scope.add = function (x) {

        myService.setTotal(x)
    }

 });

and my popupCtrl controller is now this :

module.controller('popUpCtrl', function ($scope, myService) {


    $scope.total = myService.getTotal();

})

{{total}} in my popup is now being expressed as the "0" which var total is equal to in my service. So the data is now being transferred using the service, but the setTotal() method isn't setting the var as instructed by the add() function now. I have injected my service as a dependency to both controllers.

解决方案

I did find a solution to my initial question, which was to use localStorage.setItem("num", "num") to store the data in the browser. It seems this was developed specifically to share data between tabs/windows, and so is accessible via localStorage.getItem("num").

This answers my original question, however I would now like the total to be updated dynamically. At the moment it is only updated upon refresh. Any tips would be appreciated.


EDIT (ANSWER TO ABOVE)

By using this event listener in my second popup -

window.addEventListener('storage', function (event) {
        $scope.total = parseInt(event.newValue);
        $scope.$apply();
    });

I was able to access the storage and bind it to an expression in my HTML. Bear in mind $scope.apply() needs to be called here because total is outside of the scope.

More detail here - https://developer.apple.com/library/safari/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/Name-ValueStorage/Name-ValueStorage.html

这篇关于AngularJS 帮助使用 $broadcast 在控制器之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆