AngularJS:指令中的缩小问题
我还有一个关于缩小的问题。这一次是因为$scope服务传递给指令的控制器。见以下代码:AngularJS:指令中的缩小问题,angularjs,angularjs-directive,minify,Angularjs,Angularjs Directive,Minify,我还有一个关于缩小的问题。这一次是因为$scope服务传递给指令的控制器。见以下代码: angular.module('person.directives'). directive("person", ['$dialog', function($dialog) { return { restrict: "E", templateUrl: "person/views/person.html", replace: true, scope: { myPe
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;
}
}
}]);
如果我注释掉控制器部分,那么它工作正常
正如您所看到的,我已经为该指令使用了数组声明,因此$dialog服务即使在缩小后也可以使用。但是控制器上的$scope服务我该怎么做呢 好的,我在一个单独的文件中创建了控制器:
angular.module('person.controllers').controller('personCtrl', ['$scope', function ($scope) {
$scope.test = 3;
}]);
然后在指令中,我按名称分配控制器:
controller: 'personCtrl'
我不确定这是最好的方式。不过看起来很干净。你觉得怎么样 您需要声明一个控制器,如下所示:
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提供的解决方案可以解决这个问题,但这意味着将指令的控制器暴露给整个应用程序,这是不必要的 这将有助于,但这将意味着向整个应用程序公开指令的控制器,这是不必要的。这是我提供的解决方案:)Sam=OP;-)但是,我并不是将控制器公开给整个应用程序,而是公开给该指令所属的整个模块。但是我喜欢你的方法,我会这么做。实际上,将它暴露于一个AngularJS模块意味着任何AngularJS模块都可以访问它——这就是我将它暴露于整个应用程序的意思。AngularJS模块只有在依赖于person.controllers模块的情况下才可以访问person.controllers模块。如果他们没有这种依赖性,他们就不能访问它。不是吗?不,在当前版本的AngularJS中,这些控制器将在全球范围内,在整个应用程序中都可用。哇,这让我困惑了一段时间!谢谢