Difference in controller declaration in AngularJS

前端 未结 3 457
执念已碎
执念已碎 2021-01-21 06:01

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

  1. appmodule.controller(\'Actrl\',[\'$scope\',function($scope) {}]);
3条回答
  •  轮回少年
    2021-01-21 06:34

    [EDIT] For your first case, this isn't the right syntax. The right one would be to encapsulate in the same array your dependency injection and your controller like the following:

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

    The difference between both of your definitions is that in the first case you're explicitly specifying your injection dependencies. This will avoid to rename variables name during minification which would break your code. Hence the name in quotes [i.e. those strings] will be used in the minified versions.

    Both approach are doing the same thing but the second one is just a syntactic sugar of the first one.

提交回复
热议问题