Home  >  Article  >  php教程  >  A brief discussion on the pitfalls of angularjs module returning objects

A brief discussion on the pitfalls of angularjs module returning objects

高洛峰
高洛峰Original
2016-12-09 14:07:351223browse

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=[
                           '',
                           '',
                       item.name,
                         ''
                          ].join('');
                    }else if(item.history){
                        sub=[
                           '',
                             '',
                       item.name,
                         ''
                          ].join('');
                    }else if(item.sub){
                        sub=[
                           '',
                           '',
                       item.name,
                       '',
                         ''
                          ].join('');
                        subLi='';
                    }else{
                        sub=[
                           '',
                           '',
                       item.name,
                         ''
                          ].join('');
                    }
                    tableRow = tableRow+['
  • ', sub, '
  • ', 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=[
                               '',
                               '',
                           item.name,
                             ''
                              ].join('');
                        }else if(item.history){
                            sub=[
                               '',
                                 '',
                           item.name,
                             ''
                              ].join('');
                        }else if(item.sub){
                            sub=[
                               '',
                               '',
                           item.name,
                           '',
                             ''
                              ].join('');
                            subLi='';
                        }else{
                            sub=[
                               '',
                               '',
                           item.name,
                             ''
                              ].join('');
                        }
                        tableRow = tableRow+['
  • ', sub, '
  • ', subLi ].join(''); }); $elem[0].innerHTML = tableRow; $compile($elem.contents())($scope); }); } }; }); })(window.angular);


    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn