简体   繁体   English

Angular.js:跨多个控制器的双向数据绑定

[英]Angular.js: Two-Way data binding across multiple controllers

I have this code structure in Angular: 我在Angular中有这个代码结构:

app.controller(AlphaCtrl, function(interstellarService){

  $scope.interstellarService = {
    routeToEarth: interstellarService.get('routeToEarth'),
  }

})

app.controller(CentauriCtrl, function(interstellarService){

  $scope.interstellarService = {
    routeToEarth: interstellarService.get('routeToEarth'),
  }
})

The Interstellar Service stores in the Browser storage: 星际服务存储在浏览器存储中:

appServices.factory('interstellarService', function ($rootScope, $localStorage){
    return {
        set: function(entidy, value) {
            $localStorage[entidy] = value;
        },
        get: function(entidy) {
           if ($localStorage[entidy] !== undefined) {
              return $localStorage[entidy];
           } else return false;
        }
    }
});

Now when I change in the AlphaCtrl the routeToEarth property via a setter method I expect the CentauriCtrl to update accordingly, because the data is bound at each controller to the service, as in: 现在,当我通过setter方法更改了AlphaCtrlrouteToEarth属性时,我希望CentauriCtrl能够相应地更新,因为数据在每个控制器上绑定到服务,如:

AlphaCtrl <-- data --> interstellarService <-- data --> CentauriCtrl AlphaCtrl < - data - > interstellarService < - data - > CentauriCtrl

This is what does not work: I want to use and share the value stored in routeToEarth in my Centauri-HTML, eg 这是行不通的:我想在Centauri-HTML中使用和共享存储在routeToEarth中的值,例如

<li>
  <a>
    {{interstellarService.routeToearth}} && 'Timetravel' || 'Wormhole' }}
  </a>
</li>

What am I missing here? 我在这里错过了什么?

You need to make certain changes to get it run. 您需要进行某些更改才能使其运行。

  1. missing of quotes for AlphaCtrl and CentauriCtrl 缺少AlphaCtrlCentauriCtrl的报价
  2. missed to inject $scope for both the AlphaCtrl and CentauriCtrl controllers 错过了为AlphaCtrlCentauriCtrl控制器注入$ scope
  3. controllers and service modules were different, you mentioned it as appServices for service and app for controllers 控制器和服务模块不同,您将其称为appServices for service和app for controller

Working Demo 工作演示

Script 脚本

var appServices = angular.module('app', ['ngStorage']);

appServices.factory('interstellarService', function ($rootScope, $localStorage) {
    return {
        set: function (entidy, value) {
            $localStorage[entidy] = value;
        },
        get: function (entidy) {
            if ($localStorage[entidy] !== undefined) {
                return $localStorage[entidy];
            } else return false;
        }
    }
});

appServices.controller('AlphaCtrl', function (interstellarService, $scope) {
    interstellarService.set('routeToEarth', 'Alpha');
    $scope.interstellarService = {
        routeToEarth: interstellarService.get('routeToEarth'),
    }
    console.log('AlphaCtrl value::', $scope.interstellarService);
})

appServices.controller('CentauriCtrl', function (interstellarService, $scope) {
    interstellarService.set('routeToEarth', 'Centauri');
    $scope.interstellarService = {
        routeToEarth: interstellarService.get('routeToEarth'),
    }
    console.log('CentauriCtrl value::', $scope.interstellarService);
})

Html HTML

<div ng-controller="AlphaCtrl">
    {{interstellarService.routeToEarth}}
</div>
<div ng-controller="CentauriCtrl">
    {{interstellarService.routeToEarth}}
</div>

Putting this into my CentauriCtrl finally showed the route to earth immeadiately when I change it in AlphaCtrl . 当我在AlphaCtrl更改它时,将它放入我的CentauriCtrl终于显示了通往地球的路线。

$rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){

         $scope.interstellarService = {
             routeToEarth: interstellarService.get('routeToEarth'),
         }
    });

Tribute to this SO Question: AngularJS Bootstraps Navbar 向此SO问题致敬: AngularJS Bootstraps Navbar

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

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