简体   繁体   English

在角度不同的控制器之间共享状态

[英]sharing state between different controllers in angular

I have two controls : Left Side Navigation and the right pane that changes the content on clicking of any item on left navigation. 我有两个控件:左侧导航和右侧窗格,它们在单击左侧导航上的任何项目时更改内容。

Here is the html (angular view): 这是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>

And here is the controller 这是控制器

(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();
    }]);
})();

Also, is it best way to achieve what I intend to do in angularjs? 另外,是否是实现我打算在angularjs中完成的最佳方法? I created a service and shared the variable in the two different controllers. 我创建了一个服务,并在两个不同的控制器中共享了该变量。 However it doesn't work. 但是,它不起作用。 The content on right never gets updated on clicking any of the item on left menu. 单击左侧菜单上的任何项目,始终不会更新右侧的内容。

My answer to a previous question may help. 我对上一个问题的回答可能会有所帮助。 It uses a type of observer pattern. 它使用一种观察者模式。

AngularJs update directive after a call to service method 调用服务方法后AngularJs更新指令

Your service would change to allow all interested controller or directives to either generate or listen for certain events and access the associated data. 您的服务将更改为允许所有感兴趣的控制器或指令生成或侦听某些事件并访问关联的数据。

You need to tell your controller that the value of the tab has changed and then update it with the new value from the service. 您需要告诉控制器选项卡的值已更改,然后使用服务中的新值对其进行更新。 The second controller ( ContentSwitcher ) can not know, that the value changed and you simply assigned the string value of getTab once to this.tab . 第二控制器( ContentSwitcher )不知道,该值改变,你只需指定的字符串值getTab一次this.tab

One way to archive this automatically is changing the type of the variable inside your serivce from string to an object (eg tab = {name:'Dashboard'} ) and then call $scope.$apply after you made changes to it. 自动存档的一种方法是将您服务中的变量类型从string更改为object (例如tab = {name:'Dashboard'} ),然后在对它进行更改后调用$scope.$apply

In your first controller you still assign this.tab = service.getTab() (which will be an object) and in your view {{tab.name}} . 在您的第一个控制器中,您仍将分配this.tab = service.getTab() (将是一个对象),并在您的视图中{{tab.name}}

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

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