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

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

问题描述

我有一个控制器,它是我的页面的控制器,但我想知道是否有可能有一个 AppLevel 控制器,即可以从每个页面访问的东西......所以每个页面实际上分配了 1 个以上的控制器.

我知道我可能可以使用服务来完成此操作并注入该服务,但我希望可以分配某种应用程序级别的控制器.

如果可能,我将如何在两者之间进行通信?我假设使用依赖注入并将 applevel 控制器传递到我的主页?

有人对此有什么想法吗?

谢谢

解决方案

AngularJS 利用 JavaScript 原型继承来让作用域访问父作用域上的属性.您可以定义嵌套在 HTML 中的控制器并从子级访问父级.但是,我强烈建议您不要为您的AppCtrl"依赖这个事实.在某些情况下,您正在处理的范围将被隔离,并且不会成为有权访问 AppCtrl 范围的继承层次结构的一部分.

我建议为此创建一个服务,或者您可以将 pub/sub 与 $rootScope.$on 和 $rootScope.$broadcast 一起使用.

为了展示服务示例,我将使用 shellCtrl 和 shell service 而不是 app 来使示例更加清晰.

shell"服务的工作是允许您应用中的任何其他控制器、指令或服务与 shellController 进行交互,从而与主机视图容器进行交互.

<div ng-controller="SomeOtherCtrl"></div>

//父控制器定义在与 ng-app 相同的元素上函数 ShellCtrl($scope, shell) {//我刚刚让 shellctrl 的 $scope 可以访问 shell,但是你可以这样做//这以各种方式.$scope.shell = 外壳;}//任何其他控制器函数 SomeOtherCtrl($scope, shell) {shell.setTitle('一些标题');}//shell 服务的基本示例angular.module('myApp').factory('shell', function () {返回 {title = '没有设置标题',setTitle = 函数(标题){this.title = 标题;}}});

现在您可以在不依赖范围层次结构的情况下以分离的方式在父控制器上设置属性.

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.

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.

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?

Anyone have an idea about this?

thanks

解决方案

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.

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

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

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天全站免登陆