我在角本的代碼結合的結構:Angular.js:雙向數據跨多個控制器
app.controller(AlphaCtrl, function(interstellarService){
$scope.interstellarService = {
routeToEarth: interstellarService.get('routeToEarth'),
}
})
app.controller(CentauriCtrl, function(interstellarService){
$scope.interstellarService = {
routeToEarth: interstellarService.get('routeToEarth'),
}
})
星際Service存儲在瀏覽器中存儲:
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;
}
}
});
現在,當我改變在AlphaCtrl
routeToEarth
屬性通過setter方法我期望CentauriCtrl相應地更新,因爲數據綁定在每個控制器到服務,如:
AlphaCtrl < - 數據 - > interstellarService < - 數據 - > CentauriCtrl
這是行不通:我想用和共享存儲在routeToEarth
在我半人馬座,HTML,例如價值
<li>
<a>
{{interstellarService.routeToearth}} && 'Timetravel' || 'Wormhole' }}
</a>
</li>
我在這裏錯過了什麼?
你應該嘗試的工廠,而不是 – 2014-10-17 09:11:35
好吧,我更新了我的代碼廠仍然沒有工作。 – nottinhill 2014-10-17 09:53:15
你以前在工廠設置過嗎?此外,這個表達應該做什麼?你可以嘗試渲染{{interstellarService.routeToearth}}部分嗎? – tmaximini 2014-10-17 09:59:34