AngularJS:AppLevel控制器可能吗? [英] AngularJS:AppLevel controller possible?

查看:99
本文介绍了AngularJS:AppLevel控制器可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器,是我的网页控制器,但我想知道如果它可能有一个AppLevel控制器即东西是从访问的每一个网页...所以每一页实际上已经超过1控制器分配。

I have a controller that is the controller for my page but i was wondering if its possible to have a AppLevel controller i.e. something that is accessible from every page... so each page would actually have more than 1 controller assigned.

我知道,我也许可以与服务做到这一点,并注入该服务,但我希望的某种可分配applevel控制器。

I know i can probably do this with a service and inject the service but I was hoping for some kind of applevel controller that can be assigned.

如果这一切成为可能,如何将我的2间沟通?我使用依赖注入,只是通过applevel控制器到我的主页presume?

If this possible, how would i communicate between the 2? I presume using dependency injection and just pass the applevel controller to my main page?

任何人有这个想法?

感谢

推荐答案

AngularJS利用JavaScript的原型继承为了范围来对父范围内访问性能。可以定义嵌套在HTML中的控制器和从子访问父。不过,我会强烈建议您不要依赖这个事实你'AppCtrl。在某些情况下的范围您正在使用的将是孤立的,并不会是具有访问AppCtrl的范围继承层次的一部分。

AngularJS leverages JavaScript prototypical inheritance in order for scopes to access properties on the parent scope. You can define the controllers nested in the HTML and access the parent from the child. However I would strongly urge you not to rely on this fact for your 'AppCtrl'. In some cases the scope you are working on will be isolated and will not be a part of the inheritance hierarchy that has access to the AppCtrl's scope.

我建议创建此服务,或者你可以使用的pub / sub与$ rootScope。$上$ rootScope。$播出。

I would suggest creating a service for this, or you could use pub/sub with $rootScope.$on and $rootScope.$broadcast.

要显示的服务的例子,我会使用的话shellCtrl和shell服务的,而不是应用程序来让这个例子更清楚一点。

To show the service example I'll use the words shellCtrl and shell service instead of app to make the example a little clearer.

在'壳'服务的工作就是让你的应用程序的任何其他控制器指令或服务与shellController互动,因此,主视图容器。

The 'shell' service's job is to allow any other controller, directive or service in your app to interact with the shellController, and therefore the host view container.

<div ng-app="myApp" ng-controller="ShellCtrl">
    <div ng-controller="SomeOtherCtrl"></div>
</div>

// parent controller defined on the same element as ng-app
function ShellCtrl($scope, shell) {
    // I've just made the shell accessible to the $scope of shellctrl, but you can do
    // this in various ways.
    $scope.shell = shell;
}

// any other controller
function SomeOtherCtrl($scope, shell) {
    shell.setTitle('Some title');
}

// basic example of the shell service
angular.module('myApp').factory('shell', function () {
    return {
        title = 'No title set',
        setTitle = function (title) {
            this.title = title;
        }
    }    
});

现在你可以设置一个独立的时尚父控制器的性能无需依赖的范围层次。

Now you can set properties on the parent controller in a detached fashion without relying on the scope hierarchy.

这篇关于AngularJS:AppLevel控制器可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆