内联数组注释-为什么此代码使用两个$ scopes [英] Inline Array Annotation - Why two $scopes are used for this code

查看:70
本文介绍了内联数组注释-为什么此代码使用两个$ scopes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Angular JS文档.我无法弄清楚我在以下代码中提到的一行.谁能解释?

I'm going through Angular JS Documentation. I'm not able to figure out one line that I mentioned in the below code. Can anyone explain ?

script.js :

angular.module('scopeExample', [])
.controller('MyController', ['$scope', function($scope) { // This line
  $scope.username = 'World';

  $scope.sayHello = function() {
    $scope.greeting = 'Hello ' + $scope.username + '!';
  };
}]);

index.html :

<div ng-controller="MyController">
  Your name:
    <input type="text" ng-model="username">
    <button ng-click='sayHello()'>greet</button>
  <hr>
  {{greeting}}
</div>

我不明白这一点:

['$scope', function($scope) {}]

在这里,为什么要使用两个$scope.

Here, why two $scopes are used.

推荐答案

Angular JS-内联数组注释

Angular JS - Inline Array Annotation

它用来避免缩小问题. 缩小后,代码如下:

Its used to avoid problems on minification. After minification the code looks like:

['$scope', function(a) {}]

因此Angular知道要注入哪些依赖项.

So Angular knows which dependencies to inject.

否则看起来像

function(a){}

缩小和角度后,

不知道是哪个依赖关系.

after minification and angular did not know which dependency is meant.

您将在AngularJS Docs(依赖注入)中找到更多信息. https://docs.angularjs.org/guide/di

You will find more information in the AngularJS Docs (Dependency Injection) https://docs.angularjs.org/guide/di

这篇关于内联数组注释-为什么此代码使用两个$ scopes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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