使用角度js的不同方式

时间:2022-07-31 23:56:18

i have been working on angular js and i found that are many ways to create a module, a controller, etc...

我一直在研究角度js,我发现创建模块,控制器等的方法很多......

For example:

var app = angular.module('myApp', []);

and:

angular.module('myApp', []);

If i want to create a controller (First way):

如果我想创建一个控制器(第一种方式):

app.controller('myController', [...]);

create a controller (Second way):

创建一个控制器(第二种方式):

angular.module('myApp').controller('myController', [...]);

Really... what is the difference?

真的......有什么区别?

Thanks!

2 个解决方案

#1


0  

angular.module('myApp', []) is the setter for a module that returns the instance once it is created. Putting var app = in front just keeps a reference to the modules JavaScript object. The empty array after the module name is the dependency list. We pass in an empty dependency list to distinguish it from a getter call and let Angular know that it is a setter call and we are creating a module.

angular.module('myApp',[])是模块的setter,它在创建实例后返回实例。将var app =放在前面只保留对模块JavaScript对象的引用。模块名称后面的空数组是依赖项列表。我们传入一个空的依赖列表,以区别于getter调用,让Angular知道它是一个setter调用,我们正在创建一个模块。

angular.module('myApp') is the getter for a module and returns the JavaScript object. The difference here is that there is no dependency list so Angular knows that it is retrieving a module that has already been created.

angular.module('myApp')是模块的getter并返回JavaScript对象。这里的区别在于没有依赖项列表,因此Angular知道它正在检索已经创建的模块。

When you are in the same file it is OK the keep a reference to the module with a local variable like app but when you are creating controllers in different files you should not rely on the existence of a global variable call app and should retrieve the module with a getter call.

当你在同一个文件中时,可以使用像app这样的局部变量来保持对模块的引用,但是当你在不同的文件中创建控制器时,你不应该依赖于全局变量调用app的存在而应该检索模块有一个吸气器电话。

#2


0  

There is no difference actually, it's exactly the same.

实际上没有区别,它完全一样。

This app.controller(...) is equal to angular.module('myApp').controller(...)

这个app.controller(...)等于angular.module('myApp')。controller(...)

because you declared a variable called app which is equal to angular.module()

因为你声明了一个名为app的变量,它等于angular.module()

And the chaining thing : angular.module.controller is all of you 3 exemples combined.

链接的东西:angular.module.controller是你们所有3个例子的组合。

Putting angular.module() into a variable called app or whatever just makes the syntax easier that's all ^^

将angular.module()放入一个名为app的变量或其他只是使语法更容易的全部^^

#1


0  

angular.module('myApp', []) is the setter for a module that returns the instance once it is created. Putting var app = in front just keeps a reference to the modules JavaScript object. The empty array after the module name is the dependency list. We pass in an empty dependency list to distinguish it from a getter call and let Angular know that it is a setter call and we are creating a module.

angular.module('myApp',[])是模块的setter,它在创建实例后返回实例。将var app =放在前面只保留对模块JavaScript对象的引用。模块名称后面的空数组是依赖项列表。我们传入一个空的依赖列表,以区别于getter调用,让Angular知道它是一个setter调用,我们正在创建一个模块。

angular.module('myApp') is the getter for a module and returns the JavaScript object. The difference here is that there is no dependency list so Angular knows that it is retrieving a module that has already been created.

angular.module('myApp')是模块的getter并返回JavaScript对象。这里的区别在于没有依赖项列表,因此Angular知道它正在检索已经创建的模块。

When you are in the same file it is OK the keep a reference to the module with a local variable like app but when you are creating controllers in different files you should not rely on the existence of a global variable call app and should retrieve the module with a getter call.

当你在同一个文件中时,可以使用像app这样的局部变量来保持对模块的引用,但是当你在不同的文件中创建控制器时,你不应该依赖于全局变量调用app的存在而应该检索模块有一个吸气器电话。

#2


0  

There is no difference actually, it's exactly the same.

实际上没有区别,它完全一样。

This app.controller(...) is equal to angular.module('myApp').controller(...)

这个app.controller(...)等于angular.module('myApp')。controller(...)

because you declared a variable called app which is equal to angular.module()

因为你声明了一个名为app的变量,它等于angular.module()

And the chaining thing : angular.module.controller is all of you 3 exemples combined.

链接的东西:angular.module.controller是你们所有3个例子的组合。

Putting angular.module() into a variable called app or whatever just makes the syntax easier that's all ^^

将angular.module()放入一个名为app的变量或其他只是使语法更容易的全部^^