栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

为什么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开发人员指南-
使用严格依赖注入》



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/640654.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号