为什么angularjs控制器声明具有这种语法结构?

为什么angularjs控制器声明具有这种语法结构?,第1张

为什么angularjs控制器声明具有这种语法结构?

数组语法将帮助您缩小/缩小

js
代码

angular.module('7minWorkout').controller('WorkoutController',   function ($scope, $interval, $location) {    // pre here});

将被缩小并修改为:

angular.module('7minWorkout').controller('WorkoutController',  function (a, b, c) {    // pre here});

因此Angular将无法确定要注入哪些依赖项

另一方面,使用

array
声明:

angular.module('7minWorkout').controller('WorkoutController',  ['$scope', '$interval', '$location', function ($scope, $interval, $location) {    // pre here}]);

将缩小为:

angular.module('7minWorkout').controller('WorkoutController',   ['$scope', '$interval', '$location', function (a, b, c) {    // pre here}]);

所以角就知道了

a
b
c
代表。


如果您使用第一个示例代码,则还有另一种注入变量的方法,如下所示:

WorkoutController.$inject = ['$scope', '$interval', '$location'];

要么

angular.module('7minWorkout').controller('WorkoutController',   function ($scope, $interval, $location) {   // pre here});

$inject
注释代码时,它将创建上面提到的方法。


因此,主要有四种注释:

  1. 隐式注释 -第一个示例代码
  2. 内联数组注释 -第二个示例代码
  3. $ inject属性注释 -$ inject方法
  4. $ ngInject注释注释- @ngInject方法

ng-注释

ng-annotate之类的工具可让您在应用中使用隐式依赖项注释,并在最小化之前自动添加内联数组注释。如果您决定采用这种方法,则可能要使用

ng-strict-di

有关更多信息,请参阅《AngularJS开发人员指南-
使用严格依赖注入》



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5646457.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存