如果包装在IIFE(Angular)中,JS脚本不运行

时间:2015-04-19 06:55:55

标签: javascript angularjs

这可能是一个Angular问题,但我怀疑它与一般JS有关。我正在设置一个Angular应用程序,并且到目前为止运行良好。

我已将其分解为不同的模块appapp.coreapp.components等。

我现在遇到了一个奇怪的问题,我试图在所有模块中创建一个指令,并且在这个问题上有点难过。我可以按照它应该的方式工作,模板显示在我的视图中:

    'use strict';
    angular.module('app.components')
    .directive('navBar', function() {
        return {
            controller: 'Main',
            controllerAs: 'vm',
            link: function(){
              console.log('Directive link')
            },
            template: 'Directive template'
        };
    });

但是,当我把它包装在IIFE中时:

    (function() {
        'use strict';
        angular.module('app.components')
        .directive('navBar', function() {
            return {
                link: function(){
                  console.log('Directive link')
                },
                template: 'Directive template'
            };
        });
     });

它失败了,奇怪。我的所有其他模块都以与第二个示例相同的方式编写,并且工作正常。

非常感谢任何指导。

谢谢,

戴夫

1 个答案:

答案 0 :(得分:2)

需要调用闭包才能运行其代码:

(function () {...}());

(function () {...})();

相关问题