[英]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方法更改了
AlphaCtrl
的routeToEarth
属性时,我希望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. 您需要进行某些更改才能使其运行。
AlphaCtrl
and CentauriCtrl
AlphaCtrl
和CentauriCtrl
的报价 AlphaCtrl
and CentauriCtrl
controllers AlphaCtrl
和CentauriCtrl
控制器注入$ scope appServices
for service and app
for controllers appServices
for service和app
for controller 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.