javascript 全局定义的 AngularJS 控制器和封装

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13362921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:32:58  来源:igfitidea点击:

Globally defined AngularJS controllers and encapsulation

javascriptangularjsencapsulation

提问by Andrew Rhyne

According to AngularJS's tutorial, a controller function just sits within the global scope.

根据 AngularJS 的教程,控制器函数只是位于全局范围内。

http://docs.angularjs.org/tutorial/step_04

http://docs.angularjs.org/tutorial/step_04

Do the controller functions themselves automatically get parsed into an encapsulated scope, or do they dwell within the global scope? I know that they are passed a reference to their own $scope, but it appears that the function themselves are just sitting in the global scope. Obviously this can cause problems down the road, and I have learned through experience and education to encapsulate Further more, if they do dwell within the global scope, would it not be considered a best practice to encapsulate them within an object to be referenced like this:

控制器函数本身是否会自动解析为封装的范围,或者它们是否驻留在全局范围内?我知道他们被传递了一个对他们自己的 $scope 的引用,但看起来函数本身只是位于全局范围内。显然这可能会导致问题,我通过经验和教育学会了封装 此外,如果它们确实存在于全局范围内,那么将它们封装在像这样被引用的对象中是否不被认为是最佳实践:

    Object.functionName();

Rather than this:

而不是这样:

    functionName();

So as to prevent issues that occur with the pollution of the global scope (ie overriding functions, etc..)

以防止因全局作用域(即覆盖函数等)的污染而出现的问题。

回答by pkozlowski.opensource

AngularJS supports 2 methods of registering controller functions - either as globally accessible functions (you can see this form in the mentioned tutorial) or as a part of a modules (that forms a kind of namespace). More info on modules can be found here: http://docs.angularjs.org/guide/modulebut in short one would register a controller in a module like so:

AngularJS 支持 2 种注册控制器函数的方法——作为全局可访问的函数(你可以在提到的教程中看到这种形式)或作为模块的一部分(形成一种命名空间)。关于模块的更多信息可以在这里找到:http: //docs.angularjs.org/guide/module但简而言之,可以在模块中注册一个控制器,如下所示:

angular.module('[module name]', []).controller('PhoneListCtrl', function($scope) {

  $scope.phones = [..];

  $scope.orderProp = 'age';
});

AngularJS uses a short, global-function form of declaring controllers in many examples but while this form is good for quick samples it rather shouldn't be used in real-life applications.

AngularJS 在许多示例中使用了一种简短的全局函数形式来声明控制器,虽然这种形式适用于快速示例,但它不应该用于现实生活中的应用程序

In short: AngularJS makes it possible to properly encapsulate controller functions but also exposes a simpler, quick & dirty way of declaring them as global functions.

简而言之:AngularJS 使正确封装控制器功能成为可能,但也公开了一种更简单、快速和肮脏的方式来将它们声明为全局函数。

回答by user1

You can register a controller as part of a module, as answered by pkozlowski-opensource.

您可以将控制器注册为模块的一部分,如pkozlowski-opensource所回答。

If you need minification you can simply extend this by providing the variable names before the actual function in a list:

如果您需要缩小,您可以通过在列表中的实际函数之前提供变量名称来简单地扩展它:

angular.module('[module name]', []).
  controller('PhoneListCtrl', ['$scope', function($scope) {

    $scope.phones = [..];
    $scope.orderProp = 'age';
  }]);

This will work the same after "minification":

这将在“缩小”后工作相同:

angular.module('[module name]', []).
  controller('PhoneListCtrl', ['$scope', function(s) {

    s.phones = [..];
    s.orderProp = 'age';
  }]);

This notation can be found under "Inline Annotation" at Dependency Injection.

这个符号可以在Dependency Injection 的“内联注释”下找到。

To test a controller, that has been registered as part of a module, you have to ask angular to create your controller. For example:

要测试已注册为模块一部分的控制器,您必须请求 angular 来创建您的控制器。例如:

describe('PhoneListCtrl test', function() {
  var scope;
  var ctrl;

  beforeEach(function() {
    module('[module name]');
    inject(function($rootScope, $controller) {
      scope = $rootScope.$new();
      ctrl = $controller('[module name]', {$scope: scope});
    });
  });

  it('should be ordered by age', function() {
    expect(scope.orderProp).toBe('age');
  });

});

This method of testing the controller can be found under "Testing Controllers" at Understanding the Controller Component.

这种测试控制器的方法可以在了解控制器组件的“测试控制器”下找到。