Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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
Angularjs 从指令内部访问主控制器的作用域_Angularjs_Angularjs Directive_Angularjs Scope - Fatal编程技术网

Angularjs 从指令内部访问主控制器的作用域

Angularjs 从指令内部访问主控制器的作用域,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,假设以下蓝图代码: <div ng-controller="myCtrl"> <div ng-repeat="..."> <div ng-repeat="..."> <div ng-repeat="..."> <div ng=if="..." my-directive> </div> <

假设以下蓝图代码:

<div ng-controller="myCtrl">
    <div ng-repeat="...">
        <div ng-repeat="...">
            <div ng-repeat="...">
                <div ng=if="..." my-directive>
                </div>
            </div>
        </div>
    </div>
</div>    

myApp.directive('myDirective',  function() {
    return {                   
        controller: function($scope){
            console.log('controller scope');
            console.log($scope);
        },  
        link:function(scope,element){ 
            console.log('link scope');
            console.log(scope);    
        }
    }
});

控制台中的两个输出都将指向ng if指令创建的范围。我的问题是如何从指令内部访问myCtrl的作用域。当然不是通过使用$parent。$parent….

使用服务在角度组件之间共享数据。这个问题可能是一个好的开始:。这种方法也适用于在控制器和指令之间共享数据

当您创建指令时,返回函数称为DDO指令定义对象。它的一个属性是“范围”。如果使用scope:true初始化它,则该指令将典型地继承父范围。如果将scope设置为false,则指令将使用父范围。最后,如果设置scope:{…},它将创建一个独立的作用域

var app=角度模块测试,[]; app.controllermycontrl,函数$scope{ $scope.text=控制器范围内的Im; }; app.directivemyDirective,函数{ 返回{ 限制:EA, 范围:正确, 模板:你在哪里,指令?{{text} }; }; 氢{ 光标:指针; } .指令{ 边框:5px实心F5BF6E; 填充:10px; } 你在哪里?{{text}}
最简单的方法是在指令中使用require,如:

<div ng-controller="MyCtrl">
   <div my-directive></div>
</div>


var myApp = angular.module("app", []);

myApp.controller("MyCtrl", function($scope) {
    this.text = "I am in Controller Scope";
    this.getValue = function() { return this.text; };
});

myApp.directive("myDirective", function() {
    return {
        require: "^ngController",
        link: function(scope, elem, attrs, ngCtrl) {
            elem.text(ngCtrl.getValue());
        }
    };
});

是的,你是对的。然而,我忘了提到我的蓝图不准确,因为我使用的是ui路由。因此,没有ng控制器。整个页面通过ui路由绑定到myCtrl。对那个案子有什么想法吗?感谢you@ILIAS,我更新了答案,并添加了一个与指令绑定的范围。我希望这能解决它。让我知道……如果你读了我的答案,会对你有帮助的。如果您继承了作用域,您可以访问父作用域,在这种情况下,它是您控制器的作用域。@ILIAS,这就是您要找的吗?从指令访问控制器的作用域。。。。
<div ng-controller="MyCtrl as vm">
    <my-directive on-get-value="vm.getValue()">
    </my-directive>
 </div>

angular.module('app', [])
.controller('MyCtrl', function($window) {
    var vm = this;
    vm.getValue = function() { $window.alert("I am in Controller Scope"); };
})
.directive('myDirective', function() {
  return {
    scope: {
       onGetValue:'&'
    },
    controllerAs:'vm',
    controller: function($scope) {
         $scope.onGetValue();
    }
  };
});