Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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 角度:从单独的控制器调用指令控制器函数_Javascript_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript 角度:从单独的控制器调用指令控制器函数

Javascript 角度:从单独的控制器调用指令控制器函数,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,假设我有一个叫做myApp的应用程序。菜单组件有一个指令。此指令上定义了一个控制器。现在,从另一个加载视图的控制器,我想调用该菜单指令的控制器的方法 这样做的代码是什么样子的?其他控制器如何在菜单指令的控制器上调用方法?我认为问题在于,当您应该将函数绑定到某个委托调用时,您正试图执行controller->directive,而该委托调用会执行controller,这会造成代码气味。如果两个控制器设计正确,它们就不需要互相调用。他们应该监控模型的变化并做出反应。你到底想实现什么?作为一个菜单指令

假设我有一个叫做myApp的应用程序。菜单组件有一个指令。此指令上定义了一个控制器。现在,从另一个加载视图的控制器,我想调用该菜单指令的控制器的方法


这样做的代码是什么样子的?其他控制器如何在菜单指令的控制器上调用方法?

我认为问题在于,当您应该将函数绑定到某个委托调用时,您正试图执行
controller->directive
,而该委托调用会执行
controller,这会造成代码气味。如果两个控制器设计正确,它们就不需要互相调用。他们应该监控模型的变化并做出反应。你到底想实现什么?作为一个菜单指令,Chris更可能想对用户的行为做出反应。
.directive('tab', ['$parse', function($parse) {
  return {
    require: '^tabset',
    restrict: 'EA',
    replace: true,
    templateUrl: 'template/tabs/tab.html',
    transclude: true,
    scope: {
      active: '=?',
      heading: '@',
      onSelect: '&select', //This callback is called in contentHeadingTransclude
                          //once it inserts the tab's content into the dom
      onDeselect: '&deselect'
    },
    // etc.
$scope.selectedTab = function(){ alert('woohoo'); };
<tab select="selectedTab()">
.directive('tab', ['$parse', function($parse) {
  return {
    require: '^tabset',
compile: function(elm, attrs, transclude) {
  return function postLink(scope, elm, attrs, tabsetCtrl) {
    scope.$watch('active', function(active) {
      if (active) {
        tabsetCtrl.select(scope);
      }
    });

    scope.disabled = false;
    if ( attrs.disabled ) {
      scope.$parent.$watch($parse(attrs.disabled), function(value) {
        scope.disabled = !! value;
      });
    }

    scope.select = function() {
      if ( !scope.disabled ) {
        scope.active = true;
      }
    };

    tabsetCtrl.addTab(scope);
    scope.$on('$destroy', function() {
      tabsetCtrl.removeTab(scope);
    });

    //We need to transclude later, once the content container is ready.
    //when this link happens, we're inside a tab heading.
    scope.$transcludeFn = transclude;
  };
}