为什么'window.angular'在这个函数定义中使用如此?

时间:2021-03-29 17:07:30

I'm trying to understand an angularjs file I need to use to integrate with Django, and it has a weird syntax I'm not familiar with (bear in mind I'm a junior dev, so this may be your bread and butter)...

我正在尝试理解我需要用来与Django集成的angularjs文件,它有一个我不熟悉的奇怪语法(记住我是一个初级开发者,所以这可能是你的面包和黄油) ...

It goes something like:

它类似于:

(function(angular, undefined){
    'use script';
    var djng_forms_module = angular.module('ng.django.forms', []);
    funtion hasCode(s){
        return .....
    }
    var foo = .....
}(window.angular));

I've seen this about the javascript concept of a window and it shows that the window part is the top (?) level object the browser creates when it loads the page.

我已经看到了关于窗口的javascript概念,它显示窗口部分是浏览器在加载页面时创建的*(?)级别对象。

Running console.log(window.angular) prints out a load of internal angular stuff. So I'm guessing that is the internal guts of AngularJS...?

运行console.log(window.angular)打印出一堆内部角度内容。所以我猜这是AngularJS的内部胆量......?

But why this weird encapsulation as a function (something to do with JavaScript being a 'functional' language)?

但是为什么这个奇怪的封装作为一个函数(与JavaScript作为'功能'语言有关)?

The full script is here and I can't figure out why it uses window.angular in this function definition (as opposed to the normal way of doing things). It seems like this set up means it's not working for my application when I import it via script tags.

完整的脚本在这里,我无法弄清楚为什么它在这个函数定义中使用window.angular(而不是正常的做事方式)。看起来这个设置意味着当我通过脚本标签导入它时,它不能用于我的应用程序。

1 个解决方案

#1


8  

window.angular is the global angularjs variable which is created once angularjs has been fully loaded from a script tag. The code fragment you have pasted ensures that it is executed after the population of this variable. One reason it might be written in that verbose way is simply its auto-generated nature. In a wider context, it may have implication to the order in which scripts are executed or to using different versions of the angularjs library.

window.angular是全局angularjs变量,一旦angularjs从脚本标记完全加载,就会创建该变量。您粘贴的代码片段可确保在此变量填充后执行。它可能以那种冗长的方式编写的一个原因就是其自动生成的性质。在更广泛的上下文中,它可能暗示脚本执行的顺序或使用angularjs库的不同版本。

#1


8  

window.angular is the global angularjs variable which is created once angularjs has been fully loaded from a script tag. The code fragment you have pasted ensures that it is executed after the population of this variable. One reason it might be written in that verbose way is simply its auto-generated nature. In a wider context, it may have implication to the order in which scripts are executed or to using different versions of the angularjs library.

window.angular是全局angularjs变量,一旦angularjs从脚本标记完全加载,就会创建该变量。您粘贴的代码片段可确保在此变量填充后执行。它可能以那种冗长的方式编写的一个原因就是其自动生成的性质。在更广泛的上下文中,它可能暗示脚本执行的顺序或使用angularjs库的不同版本。