By splitting different components in the module into different js files, I found a strange problem in the module during assembly. I wonder if it is a bug in AngularJS. No explanation has been found so far.
The problem is this. According to understanding, angular.module('app.main', []); is equivalent to returning an app object from the app.main namespace. Then, no matter in any js file, the pointer stored in the app variable I obtained through this method should point to the only heap memory, and the app object is stored in this memory. There is indeed no problem with this kind of operation in the js file of the module, the js file of the controller, and the js file of the service. They are the same object. But when adding the directive, the app object was not registered by the module. Why is it not registered? The conclusion is naturally that the object pointed to by the returned app variable is no longer the one we registered.
That is, there will be problems if you write it like this:
app.js
(function(angular){ angular.module('app.main', ['app.login'] ); })(window.angular);
menuController.js
(function(angular){ angular.module('app.main', []); .controller('MenuController',function($scope,menuService,userService){ var loginname=Cookies.getCookieValue("loginname"); var token=Cookies.getCookieValue("token"); Cookies.delCookieValue("token"); Cookies.delCookieValue("loginname"); alert(userService.getToken()); $scope.menu=[]; menuService.initMenu(loginname,token,function(menu){ $scope.menu=menu; $scope.$broadcast("menuLoaded"); }); $scope.displaySwitch=function(index){ if($scope.menu[index].isShow) $scope.menu[index].isShow=false; else $scope.menu[index].isShow=true; }; }); })(window.angular);
menu.js
(function(angular){ if(!app) app={}; if(!app.main) angular.module('app.main', []); .directive('menu', function($compile) { return { restrict: 'A', replace: false, priority: 999, link: function ($scope, $elem, attrs) { $scope.$on("menuLoaded", function (event, args) { var tableRow = ""; angular.forEach($scope.menu, function (item) { var sub=''; var subLi=''; if(item.main){ sub=[ '<a href="'+item.url+'" class="home-icon">', '<span class="glyphicon glyphicon-home" aria-hidden="true"></span>', item.name, '</a>' ].join(''); }else if(item.history){ sub=[ '<a href="'+item.url+'" class="sub-icon">', '<span class="glyphicon glyphicon-home glyphicon-hourglass" aria-hidden="true"></span>', item.name, '</a>' ].join(''); }else if(item.sub){ sub=[ '<a href="#" class="menu1" ng-click="displaySwitch('+item.index+')">', '<span class="glyphicon glyphicon-film" aria-hidden="true"></span>', item.name, '<span class="glyphicon glyphicon-menu-down" aria-hidden="true"></span>', '</a>' ].join(''); subLi='<ul class="cl-effect-2" ng-show="menu['+item.index+'].isShow">'; for(var i=0;i<item.sub.length;i++){ subLi=subLi+['<li>', '<a href="'+item.sub[i].url+'">', item.sub[i].name, '</a>', '</li>' ].join(''); } subLi=subLi+'</ul>'; }else{ sub=[ '<a href="'+item.url+'" class="sub-icon">', '<span class="glyphicon glyphicon-film" aria-hidden="true"></span>', item.name, '</a>' ].join(''); } tableRow = tableRow+['<li ', item.main ? 'class="active"' : '', '>', sub, '</li>', subLi ].join(''); }); $elem[0].innerHTML = tableRow; $compile($elem.contents())($scope); }); } }; }); })(window.angular);
If you load this at the same time Three js will have the problems mentioned before. Loading app.js and menuController.js or app.js and menu.js respectively will not cause problems.
But once you know the cause of the problem, you can solve the problem. Give the application of the returned app object to the global variable. Each js will judge whether this global variable exists. If it exists, use this variable. Otherwise, obtain it through the module.
app.js
(function(angular){ app={}; app.main=angular.module('app.main', ['app.login'] ); })(window.angular);
menuController.js
(function(angular){ if(!app) app={}; if(!app.main) app.main=angular.module('app.main', []); app.main.controller('MenuController',function($scope,menuService,userService){ var loginname=Cookies.getCookieValue("loginname"); var token=Cookies.getCookieValue("token"); Cookies.delCookieValue("token"); Cookies.delCookieValue("loginname"); alert(userService.getToken()); $scope.menu=[]; menuService.initMenu(loginname,token,function(menu){ $scope.menu=menu; $scope.$broadcast("menuLoaded"); }); $scope.displaySwitch=function(index){ if($scope.menu[index].isShow) $scope.menu[index].isShow=false; else $scope.menu[index].isShow=true; }; }); })(window.angular);
menu.js
(function(angular){ if(!app) app={}; if(!app.main) app.main=angular.module('app.main', []); app.main.directive('menu', function($compile) { return { restrict: 'A', replace: false, priority: 999, link: function ($scope, $elem, attrs) { $scope.$on("menuLoaded", function (event, args) { var tableRow = ""; angular.forEach($scope.menu, function (item) { var sub=''; var subLi=''; if(item.main){ sub=[ '<a href="'+item.url+'" class="home-icon">', '<span class="glyphicon glyphicon-home" aria-hidden="true"></span>', item.name, '</a>' ].join(''); }else if(item.history){ sub=[ '<a href="'+item.url+'" class="sub-icon">', '<span class="glyphicon glyphicon-home glyphicon-hourglass" aria-hidden="true"></span>', item.name, '</a>' ].join(''); }else if(item.sub){ sub=[ '<a href="#" class="menu1" ng-click="displaySwitch('+item.index+')">', '<span class="glyphicon glyphicon-film" aria-hidden="true"></span>', item.name, '<span class="glyphicon glyphicon-menu-down" aria-hidden="true"></span>', '</a>' ].join(''); subLi='<ul class="cl-effect-2" ng-show="menu['+item.index+'].isShow">'; for(var i=0;i<item.sub.length;i++){ subLi=subLi+['<li>', '<a href="'+item.sub[i].url+'">', item.sub[i].name, '</a>', '</li>' ].join(''); } subLi=subLi+'</ul>'; }else{ sub=[ '<a href="'+item.url+'" class="sub-icon">', '<span class="glyphicon glyphicon-film" aria-hidden="true"></span>', item.name, '</a>' ].join(''); } tableRow = tableRow+['<li ', item.main ? 'class="active"' : '', '>', sub, '</li>', subLi ].join(''); }); $elem[0].innerHTML = tableRow; $compile($elem.contents())($scope); }); } }; }); })(window.angular);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
