将Angular指令文件包含到index.html中可防止运行一个指令

时间:2017-02-07 09:01:29

标签: angularjs angularjs-directive

我在索引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'
]);

1 个答案:

答案 0 :(得分:2)

您正在宣布您的模块两次:

template<typename T>
struct element_ref {
    const int& i;
    const int& j;
    T& val;

    element_ref(element& e) : i(e.i), j(e.j), val(e.val) {}
};

您只能声明一次,然后在没有第二个参数的情况下使用它

val