AngularJS:指令中的缩小问题

Sam*_*Sam 32 minify angularjs angularjs-directive

我还有另一个缩小问题.这次是因为传递给指令控制器的$ scope服务.见下面的代码:

angular.module('person.directives').
directive("person", ['$dialog', function($dialog) {
return {
    restrict: "E",
    templateUrl: "person/views/person.html",
    replace: true,
    scope: {
        myPerson: '='
    },     
    controller: function ($scope)
    {                   
        $scope.test = 3;                   
    }
}
}]);
Run Code Online (Sandbox Code Playgroud)

如果我注释掉控制器部分,那么它工作正常.

正如您所看到的,我已经使用了该指令的数组声明,因此即使在缩小之后,Angular也会知道$ dialog服务.但是我应该如何为控制器上的$ scope服务做呢?

pko*_*rce 75

您需要按如下方式声明控制器:

controller: ['$scope', function ($scope)
    {                   
        $scope.test = 3;                   
    }]
Run Code Online (Sandbox Code Playgroud)

完整的例子:

angular.module('person.directives').
directive("person", ['$dialog', function($dialog) {
return {
    restrict: "E",
    templateUrl: "person/views/person.html",
    replace: true,
    scope: {
        myPerson: '='
    },     
    controller: ['$scope', function ($scope)
    {                   
        $scope.test = 3;                   
    }]
}
}]);
Run Code Online (Sandbox Code Playgroud)

@Sam提供的解决方案可以解决,但这意味着将指令的控制器暴露给整个应用程序,这是不必要的.