AngularJS 2.0 中注释的确切目的是什么

What is the exact purpose of Annotations in AngularJS 2.0

我学习 AngularJs 2.0 有一段时间了。

我对一个新引入的概念感到很困惑,Annotations

谁能告诉我注释的目的是什么?

AngularJS 中的注释用于避免在使用 运行 Grunt 或 Gulp 等工具对其进行缩小和丑化后破坏代码。

例如,如果您有:

myModule.controller('myController', function($scope){
    $scope.cars = ['toyota', 'honda'];
});

在 运行 构建工具之后,您的代码将中断,因为匿名函数中的 $scope 参数将变得丑陋,让我们说 ab 然后中断对 $scope.cars.

的引用

要解决此问题,您必须 annotate 您的代码...

myModule.controller('myController', ['$scope', function($scope){
        $scope.cars = ['toyota', 'honda'];
}]);

...这将使 $scope 参数保持不变并且不会破坏您的程序。

有解决办法。有一个名为 ngAnnotate 的 NPM 包,它将为您注释 Angular 代码。