区别在AngularJS控制器声明 [英] Difference in controller declaration in AngularJS

查看:182
本文介绍了区别在AngularJS控制器声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过控制器如下两种方式被宣布。但是,这是否做出什么差异?

I have seen controller being declared in two ways as below. But what diff does this make?


  1. appmodule.controller('Actrl',['$范围',函数($范围内){}]);

  2. appmodule.controller('Actrl',函数($范围内){});

  1. appmodule.controller('Actrl',['$scope',function($scope) {}]);
  2. appmodule.controller('Actrl',function($scope) {});

但是,大部分的时间,第1不起作用。为什么呢?

But, most of the times, the 1st doesn't work. Why?

推荐答案

这两种语法都是相同的,但第一个是preferred <击>(有一个错字,请参见下面的说明)如果您被污染减量的code。

Both the syntax are same but the first one is preferred (there is a typo, see below description) if you are minifying your code.

角解析基于名称的依赖,所以当你写 appmodule.controller('Actrl',函数($范围内){}); 语法,角的注入通过读取参数名的 $范围依赖 $范围。但是当你的code已经过压缩生产水平,然后用你的code会变成这样的:

Angular resolves the dependency based on the name so when you write appmodule.controller('Actrl',function($scope) {}); syntax, Angular injects the dependency of $scope by reading the argument name $scope. But when your code is minified for production level use then your code will become like:

appmodule.controller('Actrl', function(a) {});

现在,角度将不能够解决与名称 A 的依赖。这就是为什么第一种方法,即使用 appmodule.controller('Actrl',['$范围',函数($范围内){}]); 。现在,当你code最小化生产,你的code将是这样的:

Now, the Angular will not be able to resolve the dependency with the name a. That is why the first approach is used i.e. appmodule.controller('Actrl',['$scope', function($scope) {}]);. Now when your code is minimized for production, your code will be like this:

 appmodule.controller('Actrl',['$scope', function(a) {}]);

现在,角可以匹配该指数基于位置 $范围

<击>。在你的code错字的地方名单应该不是函数之前关闭声明。

阅读依赖注解的主题下这方面的详细信息。<强>内嵌阵列注释

Read the Dependency Annotation for more information on this under the topic Inline Array Annotation.

角调用某些功能(如服务工厂
  控制器)通过喷射器。您需要标注这些功能,以便
  喷射器知道哪些服务要注入的功能。
  没有与服务名称标注您的code的三种方式
  信息:

Angular invokes certain functions (like service factories and controllers) via the injector. You need to annotate these functions so that the injector knows what services to inject into the function. There are three ways of annotating your code with service name information:


      
  • 使用内联阵列注释(preferred)

  •   
  • 使用$注入性的注释

  •   
  • 从隐函数参数名称(有警告)

  •   

编辑:
一个音符上,缩小:在两个不同风格的另一个更详细的说明

由于角推断控制器的依赖关系的名字
  参数传递给控制器​​的构造函数,如果你要
  压缩JavaScript code代表PhoneListCtrl控制器,其所有
  函数的参数会被缩小的同时,和依赖
  喷射器将无法正确地识别服务

Since Angular infers the controller's dependencies from the names of arguments to the controller's constructor function, if you were to minify the JavaScript code for PhoneListCtrl controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly.

我们可以通过与名称注释功能克服这个问题
  的相关性,作为字符串提供,这将不会被缩小的。
  有两种方式来提供这些注入注释

We can overcome this problem by annotating the function with the names of the dependencies, provided as strings, which will not get minified. There are two ways to provide these injection annotations:

这篇关于区别在AngularJS控制器声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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