简体   繁体   English

多个 ng-app 的相同控制器

[英]Same controller for multiple ng-app

I have a single webpage, that have different documents.php, each document with its ng-app and some controllers.我有一个网页,有不同的documents.php,每个文件都有它的ng-app 和一些控制器。 (My webpage is similat to spotify). (我的网页类似于 Spotify)。

player.php: (Where the user can play some music with an audio player) player.php:(用户可以使用音频播放器播放一些音乐)

<div class="navbar-fixed" ng-controller="menuController as menu2">
<?php include('includes/menu.php'); ?>
</div>
<div ng-controller="InteractiveController as interactCtrl">
//some code
</div>

panel_admin.php: (where the user can modify its data and some data of the webpage) panel_admin.php:(用户可以在这里修改自己的数据和网页的一些数据)

<div class="navbar-fixed" ng-controller="menuController as menu2">
<?php include('includes/menu.php'); ?>
</div>
<div ng-controller="AdminController as AdminCtrl">
//some code
</div>

player.php and panel_admin.php have its respective player.js and panel_admin.js with its .controllers. player.php 和 panel_admin.php 有各自的player.js 和 panel_admin.js及其.controllers

player.js:播放器.js:

var decibelsInteractive = angular.module("decibels-interactive", []);
decibelsInteractive.controller('InteractiveController', function ($scope, $log) {

panel_admin.js: panel_admin.js:

var adminControl = angular.module("decibels-admin", []);
adminControl.controller("AdminController", function($scope){

Now, I have the menu bar in menu.php, where I use in player.php , and in panel_admin.php document.现在,我在 menu.php 中有菜单栏,我在player.phppanel_admin.php文档中使用。 I import it because I don't want to repeat code in every page where I need to use the menu (menu.php have options like manage user options, logout, and many others...).我导入它是因为我不想在需要使用菜单的每个页面中重复代码(menu.php 有管理用户选项、注销等选项......)。 I also have created a menu.js to only have the specific methods in one document and not in 25 documents.我还创建了一个menu.js ,只在一个文档中而不是在 25 个文档中包含特定方法。

menu.php: (menu is a menu bar where the user have some options, and it also have its username and logout option) menu.php: (menu 是一个菜单栏,用户在其中有一些选项,也有用户名和注销选项)

//this document only contains some html code

Now, I've tried to implement a simple code (in menu.js ) that allows me to share one controller when I have many ng-app.现在,我尝试实现一个简单的代码(在menu.js 中),当我有很多 ng-app 时,它允许我共享一个控制器。

Question with the code I've taken 问题与我已经采取的代码

menu.js:菜单.js:

angular.module('decibels-interactive', ['shared']); //player2.php
angular.module('decibels-admin', ['shared']); //panel_admin.php
var sharedModule=angular.module('shared',[]);

sharedModule.controller('menuController',['$scope',function($scope) {
alert("menuController");
}]);   
});

And this worked!!!这奏效了!!! But then, angular shows an error... (loading player.php)但是,角度显示错误...(正在加载 player.php)

Error: [ng:areq] http://errors.angularjs.org/1.2.25/ng/areq?p0=InteractiveController&p1=not%20a%20function%2C%20got%20undefined错误:[ng:areq] http://errors.angularjs.org/1.2.25/ng/areq?p0=InteractiveController&p1=not%20a%20function%2C%20got%20undefined

And when I load panel_admin the same error appears again, but with the AdminController...当我加载 panel_admin 时,同样的错误再次出现,但是使用 AdminController ...

It seems like that angular only detects the first controller in each .php, and can't find the second one.似乎 angular 只检测到每个 .php 中的第一个控制器,而找不到第二个。

What am I doing wrong?我究竟做错了什么?

Thank you!谢谢!

You can create a file for your shared code and have the controller code inside it.您可以为共享代码创建一个文件,并在其中包含控制器代码。

// @file mySharedCode.js
var MySharedCode = MySharedCode || {};

MySharedCode.menuController = function($scope) {
    // controller logic
};

Then, in your app.js file call menuController function然后,在你的 app.js 文件中调用menuController函数

// @file app.js
sharedModule.controller('menuController', MySharedCode.menuController);

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

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