AngularJS 使用捆绑/缩小

示例

将$scope注入到控制器的构造函数中的方式是一种演示和使用角度依赖注入的基本选项的方式,但由于无法将其最小化,因此尚未准备就绪。那是因为缩小系统更改了变量名,而anguar的依赖项注入使用参数名来知道必须注入什么。因此,举个例子,ExampleController的构造函数最小化为以下代码。

function n(n){this.setUpWatches(n)

并$scope更改为n!
为了克服这个问题,我们可以添加一个$inject array(string[])。这样,Angular的DI就会知道控制器构造函数在什么位置注入什么。
所以上面的打字稿变成

moduleApp.Controllers{
    class Address {
        line1: string;
        line2: string;
        city: string;
        state: string;
    }
    export class SampleController {
        firstName: string;
        lastName: string;
        age: number;
        address: Address;
        setUpWatches($scope: ng.IScope): void {
            $scope.$watch(() => this.firstName, (n, o) => {
                //n是字符串,o也是如此
            });
        };
        static $inject : string[] = ['$scope'];
        constructor($scope: ng.IScope) {
            this.setUpWatches($scope);
        }
    }
}