Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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 - Fatal编程技术网

Angularjs 访问子指令函数的父指令

Angularjs 访问子指令函数的父指令,angularjs,angularjs-directive,Angularjs,Angularjs Directive,假设您有两条指令: <navigation-extended> <navigation /> </navigation-extended> 导航扩展html模板使用div上的transclude属性插入基本导航。除此之外,导航扩展还包含额外的html(导航控件) 从父指令内部访问子功能的最佳方式是什么 我尝试过的事情: -require->使用此选项,我可以从内部导航访问导航扩展方法,但不能通过其他方式访问我认为实现这一点的最佳方法是使用$br

假设您有两条指令:

<navigation-extended>
    <navigation /> 
</navigation-extended>

导航扩展html模板使用div上的transclude属性插入基本导航。除此之外,导航扩展还包含额外的html(导航控件)

从父指令内部访问子功能的最佳方式是什么

我尝试过的事情:
-require->使用此选项,我可以从内部导航访问导航扩展方法,但不能通过其他方式访问

我认为实现这一点的最佳方法是使用
$broadcast
在父指令中触发事件,并在子指令中使用
$on
处理它,有关此问题的更多信息,请查看此答案

请尝试此简单广播



它会给你价值

function firstCtrl($scope)
{
    $scope.$broadcast('someEvent', [1,2,3]);
}

function secondCtrl($scope)
{
    $scope.$on('someEvent', function(event, mass) { console.log(mass); });
}