什么是角定义应用程序的好处? [英] What is the benefit of defining Angular app?
问题描述
在一些AngularJS教程,棱角分明的应用程序被定义为:
In some AngularJS tutorials, angular app is defined as:
myApp = angular.module("myApp",[]);
但是,我们也可以做离不开它。我可以看到的唯一区别是,当我们定义控制器,我们不能用成语:
But we can also do without it. The only difference I can see is when we define controller, we can't use idiom:
myApp.controller("myCtrl",function(){ })
但必须使用
function myCtrl (){}
是否有明确界定对myApp的任何其他好处,因为我只会创建我的网站单一的应用程序?如果我不定义对myApp,然后在我的模块连接到?
Is there any other benefits of defining myApp explicitly, given that I will only create a single app for my site? If I don't define myApp, then where my modules are attached to?
如果有,我怎么能与茉莉测试重新对myApp?
If there is, how I can recreate myApp in testing with Jasmin?
推荐答案
您可以在(至少)3种方式定义控制器:
You can define controllers in (at least) 3 ways:
-
定义控制器作为一个全球性的 VAR 的(存储在
窗口
对象)
function Ctrl() {}
这是相同的操作的方式:
which is the same as doing:
window.Ctrl = function () {}
创建一个模块,并使用返回的实例来创建新的控制器:
Create a module and use the returned instance to create new controllers:
var app = angular.module('app', []);
app.controller('Ctrl', function() {});
直接在模块上创建控制器不存储任何引用(同为 2
但没有使用增值经销商):
Create the controllers directly on the module without storing any references (the same as 2
but without using vars):
angular.module('app', []);
angular.module('app').controller('Ctrl', function() {});
从角的角度来看,他们都做同样的,你甚至可以混合在一起他们,他们会工作。唯一的区别是,选择 1
使用全局变量,而在选项 2
和 3
控制器存储一个角的私人对象中。
From Angular's point of view, they all do the same, you can even mix them together and they will work. The only difference is that option 1
uses global vars while in options 2
and 3
the controllers are stored inside an Angular's private object.
这篇关于什么是角定义应用程序的好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!