为什么全局函数认为"错误"在1.3角 [英] Why are global functions considered "wrong" in Angular 1.3

查看:147
本文介绍了为什么全局函数认为"错误"在1.3角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传统我成功我的角度code这样的

Traditionally I have managed my Angular code like this

//File 1
angular.module('name',[])
//File 2
function TestController(){

}
TestController.prototype.// inherited stuff
angular.module('name').controller('testController',TestController);

这伟大的工作,让我轻松地分区我的文件。现在,我尝试升级到1.3,并得到了臭名昭著的...

This worked great and allowed me to partition my files easily. Now I try to upgrade to 1.3 and get the infamous...

Error: [ng:areq] Argument 'TestController' is not a function, got undefined 

当然,这是由于号称清洁欲望这种变化达人的方式写code。关于这种模式是什么更复杂?是否有维持这种格局不改变全局设置?

Of course this is due to this change which claims a desire to clean up the way people write code. What about this pattern is more complex? Is there a way to maintain this pattern without changing the global settings?

推荐答案

有实际上是一个<一个href=\"https://github.com/angular/angular.js/commit/3f2232b5a181512fac23775b1df4a6ebda67d018#commitcomment-8231175\"相对=nofollow>您链接到了一个相当扎实的解释在页面上发表评论。

There is actually a comment on the page you linked to that had a fairly solid explanation.

全局控制器是指你的控制器被定义为功能
  窗口对象。这意味着它们公开提供给
  使用JavaScript的任何其他位发生这种情况定义冲突
  具有相同名称的功能。诚然,如果后期解决您的
  与...控制器那么这很可能不会发生,但控制器
  总是存在的机会,特别是如果你使用若干
  第三方库。它是更安全的把这些控制器
  模块的内部安全功能。那么你有更多的控制权
  在何时何地该模块被加载。不幸的是控制器
  名称在一个单独的应用角度和全球的,所以你还是
  有发生冲突的可能性,但至少你可以不冲突
  完全不同的code在JavaScript的全局命名空间。

Global controllers refer to your controllers being defined as function on the window object. This means that they are openly available to conflict with any other bit of JavaScript that happens to define a function with the same name. Admittedly, if you post-fix your controllers with ...Controller then this could well not happen but there is always the chance, especially if you were to use a number of 3rd party libraries. It is much safer to put these controller functions inside the safety of a module. You then have more control over when and where this module gets loaded. Unfortunately controller names are global across an individual Angular app and so you still have the potential for conflict but at least you can't clash with completely different code in the JavaScript global namespace.

这样的想法是,全球控制器功能可以与任何使用JavaScript的任何其他全局函数冲突。因此,要消除自己的code或第三方脚本冲突的机会,不使用全局控制器,让你的code更安全,更稳定。

So the idea is that global controller functions could conflict with any other global function in any javascript you use. So to eliminate the chance of a conflict with your own code or a third-party script, not using global controllers makes your code safer and more consistent.

作为由@Brett评论中提到,您可以使用IIFE在你的原型设计。 下面是一个使用该与普拉克的更新。主要的变化只是看起来是这样的。

As mentioned in the comments by @Brett, you can use IIFE around your prototyping. Here is an update of your plunk that uses that. The main change just looks like this.

(function() {
  TestController.prototype.name = 'World'
})();

这篇关于为什么全局函数认为&QUOT;错误&QUOT;在1.3角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆