简体   繁体   English

使用服务定义基本的AngularJS控制器

[英]Defining basic AngularJS controllers with services

Reading through the documentation and playing with jsfiddle, I've noticed controllers being defined (and working) in the following ways*: 通过阅读文档并使用jsfiddle进行阅读,我注意到控制器以下列方式定义(和工作)*:

function InvoiceCntl( $scope ) {...}
function Ctrl2( $scope, $timeout ) {...}
function countController1( $scope, $http, $timeout ) {...}
function countController2( $scope, $timeout, $http ) {...}

My question is about the part of the AngularJS library that calls my controller: how does it know what services I'm expecting and which order my controller expects them in? 我的问题是关于调用我的控制器的AngularJS库的一部分:它如何知道我期望的服务以及我的控制器期望它们的顺序?

It's JavaScript, Jim, but not as we know it. 它是JavaScript,Jim,但不是我们所知道的。

*bearing in mind that controllers shouldn't be defined in the global scope *请记住,控制器不应在全球范围内定义

From Angular's dev guide : 来自Angular的开发指南

How does the injector know what service needs to be injected? 注射器如何知道需要注射哪些服务?

(...) (......)

Inferring Dependencies 推断依赖关系

The simplest way to get hold of the dependencies, is to assume that the function parameter names are the names of the dependencies. 获取依赖关系的最简单方法是假设函数参数名称是依赖项的名称。

function MyController($scope, greeter) { ... } function MyController($ scope,greeter){...}

Given a function the injector can infer the names of the service to inject by examining the function declaration and extracting the parameter names. 给定一个函数,注入器可以通过检查函数声明和提取参数名称来推断要注入的服务的名称。 In the above example $scope, and greeter are two services which need to be injected into the function. 在上面的示例中,$ scope和greeter是两个需要注入函数的服务。

That doesn't remain true when your code needs to get minified. 当您的代码需要缩小时,这不会成立。 Check "$inject Annotation" section in the dev guide (link above) to better understand it. 检查开发指南(上面的链接)中的“$ inject Annotation”部分,以便更好地理解它。

By default, the AngularJS injector service will match the names of the arguments of your controller function against the names of built-in AngularJS services (starting with $ sign): 默认情况下,AngularJS注入器服务将使控制器函数的参数名称与内置AngularJS服务的名称(以$符号开头)匹配:

// Old way to define your controller
angular.module('yourModule')
    .controller('yourController', function($scope, $http, yourService){

        // Your code here

    });

However, since this is simply based on string comparison, this can cause problems when your code is uglified and/or minified. 但是,由于这只是基于字符串比较,因此当您的代码被丑化和/或缩小时,这可能会导致问题。

Therefore it is recommended that you use the newer syntax for creating controllers using the array notation like this: 因此,建议您使用较新的语法来使用数组表示法创建控制器,如下所示:

// Newer, better way to define your controller
angular.module('yourModule')
    .controller('yourController', ['$scope', '$http', 'yourService', function($scope, $http, yourService){

        // Your code here

    }]);

Notice that the controller function is replaced by an array consisting of the services you would like to inject and ending with the controller function. 请注意,控制器功能由一个数组替换,该数组包含您要注入的服务以及以控制器功能结束的服务。

AngularJS will now inject the services in the order you specified them in the array like this: AngularJS现在将按照您在数组中指定它们的顺序注入服务,如下所示:

['yourService', 'yourOtherService', function(yourService, yourOtherService){

    // You can use yourService and yourOtherService here
}]

The names don't have to correspond, so you can use: 名称不必对应,因此您可以使用:

['$http', '$scope', function(h, s){

    // You can use h and s here
    // h will be the $http service, s will be the $scope
}]

It's highly recommended to use the newer array notation because it guarantees that your code will still work after minifying or uglifying it. 强烈建议使用较新的数组表示法,因为它可以保证您的代码在缩小或丑化之后仍然有效。

Hope that helps! 希望有所帮助!

Controllers doesn't expect them to be in any order! 控制器不希望它们处于任何顺序! They only identifies them with the Name of the services which can be in any order. 他们只使用可以按任何顺序排列的服务名称来识别它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM