Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在某些情况下,AngularJS会覆盖服务注入_Javascript_Html_Angularjs_Dependency Injection - Fatal编程技术网

Javascript 在某些情况下,AngularJS会覆盖服务注入

Javascript 在某些情况下,AngularJS会覆盖服务注入,javascript,html,angularjs,dependency-injection,Javascript,Html,Angularjs,Dependency Injection,我有一个角度服务(我们称之为$superService),我在很多指令、控制器等中使用它。但我也有一个特定的指令,我希望有以下行为:如果我在该特定指令中放置任何其他指令,并且该指令在它们自己的控制器中使用$superService,我想在那里注入另一个$superService实例。让我举一个我现在是如何做到这一点的例子: function SuperService(){ this.action = function(){ // some action } } v

我有一个角度服务(我们称之为$superService),我在很多指令、控制器等中使用它。但我也有一个特定的指令,我希望有以下行为:如果我在该特定指令中放置任何其他指令,并且该指令在它们自己的控制器中使用$superService,我想在那里注入另一个$superService实例。让我举一个我现在是如何做到这一点的例子:

function SuperService(){
    this.action = function(){
        // some action
    }
}
var module = angular.module('module');
module.service('$superService', SuperService);
module.directive('oneDirective', function(){
    return {
         scope: {
             customSuperService: '='
         },
         // transulde, replace, restrict and other if needed
         controller: ['$scope', '$superService', function($scope, $superService){
             if($scope.attrs.customSuperService)
                  $superService = $scope.customSuperService;

             // code utilizing $superService
         }
         link: function($scope, $element, $attrs){
              $scope.attrs = $attrs;
         }
    };
}).directive('specificDirective', [function(){
    return {
         transclude: true,
         replace: true,
         scope: true,
         template: '<div ng-transclude></div>',
         compile: function(){
             pre: function($scope, $element, $attrs, $transclude){
                  $scope.customSuperService = new SuperService();
             }
         }
    };
}]);
功能超级服务(){
this.action=函数(){
//一些行动
}
}
变量模块=角度模块(“模块”);
服务模块(“$superService”,superService);
module.directive('oneDirective',function(){
返回{
范围:{
customSuperService:“=”
},
//如有需要,进行转运、更换、限制和其他
控制器:['$scope','$superService',函数($scope,$superService){
if($scope.attrs.customSuperService)
$superService=$scope.customSuperService;
//使用$superService的代码
}
链接:函数($scope、$element、$attrs){
$scope.attrs=$attrs;
}
};
}).directive('specifictdirective',[function(){
返回{
是的,
替换:正确,
范围:正确,
模板:“”,
编译:函数(){
前置:函数($scope、$element、$attrs、$transclude){
$scope.customSuperService=newsuperservice();
}
}
};
}]);
所以通常我会使用一个指令,如下所示:

但当我在Specific指令中使用它时,我必须这样编写标记:

     <div specific-directive>
         <div one-directive custom-super-service="customSuperService"></div>
     </div>

有没有更好的方法可以做到这一点?也许可以使用自定义服务提供商?我希望有一个解决方案,我可以这样编写标记:



没有显式地将自定义实例传递给指令。

依赖项注入配置是全局的,这意味着如果您为某个参数修改要注入的实例,它将在每个上下文中更改;事情将中断

我不太清楚您最终想要实现什么,但是如果您希望基于指令的嵌套上下文实现一些不同的行为,您总是可以通过拥有子指令所需的父控制器来影响这些行为

服务在Angular中是单例的,但是您可以让单例在不同的上下文中维护对象散列

可能存在的副本
<div specific-directive>
    <div one-directive></div>
</div>