简体   繁体   English

如何访问指令父作用域

[英]How to access directive parent scope

I am starting to learn AngularJS and while trying to use a custom directive. 我开始学习AngularJS,同时尝试使用自定义指令。

I cant seem to get the child to read the parentScope, according to angular documentation children accesible methods should be defined inside the controller of the parent directive . 根据角度文档,我似乎无法让孩子阅读parentScope,应该在parent指令控制器定义孩子可访问的方法。 It looks like the changes directives are being run, but I cant seem to access the parent scope (function not defined acording to chrome). 看起来好像正在运行change指令,但是我似乎无法访问父作用域(未根据chrome定义函数)。

Thanks in advance for any help 预先感谢您的任何帮助

plunker 矮人

That's because you are using an Isolated scope(which is good). 那是因为您使用的是隔离范围(很好)。 This statement isolates the scope from its parent 该语句将范围与其父级隔离

    scope: {
        key:'@',
        title:'@'
    },

Just pass the parent controller to the child like this. 像这样将父控制器传递给孩子。

require: '?^parentDirective'
link: function(scope, element, attrs, parentCtrl) {
   scope.parentCtrl = parentCtrl;
}

And then in the parent controller modify your could like this: 然后在父控制器中修改如下所示:

var self = this;

self.addColumn = function(data){
    $scope.columns.push(data);
};

You do not have direct access to the parent scope. 您没有直接访问父范围的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM