AngularJS:指令中的缩小问题 [英] AngularJS : minification issue in directive

查看:13
本文介绍了AngularJS:指令中的缩小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have yet another issue with minification. This time it's because of the $scope service passed to the directive's controller. See below code:

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;                   
    }
}
}]);

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

If I comment out the controller part, then it works fine.

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

As you can see, I've used the array declaration for the directive, so the $dialog service is known to Angular even after minification. But how am I supposed to do it for the $scope service on the controller ?

推荐答案

需要声明一个控制器如下:

You need to declare a controller as follows:

controller: ['$scope', function ($scope)
    {                   
        $scope.test = 3;                   
    }]

完整示例:

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;                   
    }]
}
}]);

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

A solution provided by @Sam would work to but it would mean exposing directive's controller to the whole application which is unnecessary.

这篇关于AngularJS:指令中的缩小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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