我有兩個控件:左側導航和右側窗格,用於在點擊左側導航欄上的任何項目時更改內容。以角度在不同控制器之間共享狀態
下面是HTML(斜視):
<nav class="navigation">
<ul class="list-unstyled" ng-controller="NavigationController as navigation">
<li ng-repeat="nav in navigation.tabs" class="has-submenu">
<a href="#" ng-click="navigation.changeContent(nav.name)">{{nav.name}}</a>
<ul class="list-unstyled" ng-show="nav.subNav">
<li ng-repeat="subnav in nav.subNav"><a href="#" ng-click="navigation.changeContent(subnav.name)">{{subnav.name}}</a></li>
</ul>
</li>
</ul>
</nav>
<section class="content" ng-controller="ContentSwitcher as content">
{{content.tab}}
<div class="warper container-fluid" >
<div class="container-scroll"></div>
</div>
</section>
這裏是控制器
(function() {
var app = angular.module('provisioning', []);
app.service('contentService',function(){
var tab = 'Dashboard';
return {
getTab : function(){ return tab; },
setTab : function(value){ tab = value}
}
});
app.controller('NavigationController',['contentService','$log', function(cs,log){
this.tabs = [
{
name: 'Dashboard'
},
{
name: 'Manage',
subNav: [
{
name: 'Account'
},
{
name: 'Facility'
},
{
name: 'Doctors'
},
{
name: 'Patients'
},
{
name: 'Nurses'
},
{
name: 'Device Inventory'
}
]
},
{
name: 'Health Tracker'
},
{
name: 'Reports'
},
{
name: 'Settings'
},
{
name: 'Logout'
}
];
var template = this;
this.changeContent = function(tab){
cs.setTab(tab);
}
}]);
app.controller('ContentSwitcher', ['contentService',function(cs){
this.tab = cs.getTab();
}]);
})();
而且,是要達到什麼我打算在angularjs做最好的方法是什麼?我創建了一個服務並在兩個不同的控制器中共享變量。但它不起作用。在點擊左側菜單上的任何項目時,右側的內容都不會更新。
我得到upvote建議一個合理的替代方法? – Yeager 2015-03-20 10:42:00
在這裏你去... :) – beNerd 2015-03-20 11:21:52