简体   繁体   English

AngularJS在1个模板中使用一些控制器

[英]AngularJS using some controllers in 1 template

I have diferents controllers in my app. 我的应用程序中有不同的控制器。

admin.php: admin.php的:

<body ng-controller="AdminController as AdminCtrl">

player.php: player.php:

<body ng-controller="InteractiveController as interactCtrl">

these 2 .php documents have their respective admin.js and player.js with its .controller running correctly (I have more than these 2). 这2个.php文档分别具有各自的admin.js和player.js ,其.controller正常运行(我不止这2个)。

Now, I have the menu bar in menu.php, where I use in both admin.php and player.php documents, imported with: 现在,我在menu.php中具有菜单栏,在其中使用admin.php和player.php文档, 并通过以下方式导入:

<?php include('includes/menu.php'); ?>

So, if I want to use the values of each controller (admin.js and player.js) in the menu.php , how can I do it? 因此,如果我想使用menu.php中每个控制器(admin.js和player.js) ,该怎么办?

for example, I need to do this: 例如,我需要这样做:

AdminCtrl.user.getUsername();
interactCtrl.user.getUsername();

But it is ridiculous because I would get js into an errors and if I had 500 controllers it wouldn't be recomended. 但这很荒谬,因为我会让js出错,并且如果我有500个控制器,就不会推荐它。

I thought that maybe I could do something similar to this: 我以为也许我可以做些类似的事情:

<body ng-controller="AdminController as the_same_name">
<body ng-controller="InteractiveController as the_same_name">

but I thint that all my controllers with the same name won't be a good idea. 但我想我所有同名的控制器都不是一个好主意。

What do you think? 你怎么看?

Thank you! 谢谢!

Basicly I recommend to store all the data you want to use cross-controller in one or more services, and then inject the service in your menubar controller. 基本上,我建议将要使用跨控制器的所有数据存储在一个或多个服务中,然后将该服务注入到菜单栏控制器中。 That way you can access the data you store in those services in your menubar. 这样,您可以访问菜单栏中那些服务中存储的数据。

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

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