2017-02-07 38 views
1

我在我的索引Html文件中包含两个简单指令。但奇怪的是我上面包含的文件不起作用。将Angular指令文件包含到index.html中可防止运行一个指令

如果我将这些文件分别添加到index.html,每个文件运行良好。

我的索引HTML文件:

<script src="app/directives/spinnerHide.js" type="text/javascript"></script> 
<script src="app/directives/moreInformation.js" type="text/javascript"> </script> 

在上述情况下,spinnerHide不工作。

我的指令:

(function() { 
    'use strict'; 
    var app = angular.module('app.directives', []); 
    app.directive('spHide', spHide); 

    function spHide($rootScope) { 
     var directive = { 
      restrict: 'A', 
      link: linker 
     }; 
     return directive; 

     function linker($scope, elm, attrs) { 
      $scope.$watch(function() { 
       return $rootScope.spinnerActive; 
      }, function() { 
       if ($rootScope.spinnerActive) { 
        elm.removeClass('ng-hide'); 
       } 
       else { 
        elm.addClass('ng-hide'); 
       } 
      }, true); 
     } 
     } 
    })(); 


(function() { 
    'use strict'; 
    var app = angular.module('app.directives', []); 
    app.directive('moreInformation', moreInformation); 

    moreInformation.$inject = []; 

    function moreInformation() { 
     var directive = { 
      restrict: 'A', 
      link: linker 
     }; 
     return directive; 

     function linker($scope, elm, attrs) { 

      $(elm).click(function() { 
       var contentObj = $(elm).parents().eq(2).children().eq(1); 
       var classNameArr = contentObj.attr('class').split(' '); 
       angular.forEach(classNameArr, function (value) { 
        if (value === 'ng-hide') { 
         contentObj.removeClass('ng-hide'); 
         contentObj.slideDown(); 
         $(elm).children().removeClass('fa fa-chevron-down'); 
         $(elm).children().addClass('fa fa-chevron-up'); 
        } 
        else { 
         contentObj.slideUp(); 
         contentObj.addClass('ng-hide'); 
         $(elm).children().removeClass('fa fa-chevron-up'); 
         $(elm).children().addClass('fa fa-chevron-down'); 
        } 
       }); 
      }); 
     } 
    } 
})(); 

我app.js文件:

angular.module('app', [ 
    'ui.router', 
    'ui.bootstrap', 
    'ngStorage', 
    'app.auth', 
    'angular-confirm', 
    'angular-spinkit', 
    'datatables', 
    'toastr', 
    'app.directives' 
]); 

回答

2

你两次声明你的模块:

// Passing a second parameter is declaring the module 
var app = angular.module('app.directives', []); 

你只能申报一次,然后用它没有第二个参数

// Get the module 
var app = angular.module('app.directives');