2017-07-27 66 views
0

采用可变我有2个指令:调用从孩子指令的功能和在父指令

指令A具有$scope.originalData变量,从HTTP请求获取数据,并且还具有使用该功能变量:

$scope.findStepEducatByActId = function(act_id){ 
     console.info('findfindStepEducatByActId',$scope.originalData); 
     if(angular.isDefined($scope.originalData.connections)){ 
      angular.forEach($scope.originalData.connections[0].con_detail,function(value,key){ 
       if(value.act_id_from == act_id){ 
        return value.educategory_from; 
       } 
       if(value.act_id_to == act_id){ 
        return value.educategory_to; 
       } 
      }); 
     } 
    }; 

指令B(被称为flowChart)具有该功能传递给它在A

<flow-chart findStepEducatByActId="findStepEducatByActId(act_id)"></flow-chart> 
的HTML文件模板

而且我也是在这个文件打印出来$scope.originalData{{originalData}}

指令B被简单地调用该函数:

angular.module('slmFlowClass').directive('flowChart', [function() { 
    return { 
     restrict: 'E', 
     templateUrl: 'scripts/modules/slmFlowClass/FlowClass.FlowChart.Template.html', 
     replace: true, 
     scope: { 
      findStepEducatByActId: '&' 
     }, 
    link: function($scope, $element, $attrs) { 
      $scope.showNodeTooltip = function($event, node){ 
       $scope.findStepEducatByActId('708663'); 
      } 
    } 

当我运行它,我得到一个日志:findfindStepEducatByActId undefined,但在HTML $scope.originalData我可以看到这个变量包含的数据。 所以我的问题是,为什么当我从B调用这个函数$scope.originalData为空?

回答

0

使用角度指令的'require'属性。 在你的指令B,

return { 
    restrict: 'E', 
    templateUrl: 'scripts/modules/slmFlowClass/FlowClass.FlowChart.Template.html', 
    replace: true, 
    require: 'A' 
    .... 

有关此的详细信息,请参阅角文档指令。 https://docs.angularjs.org/guide/directive

相关问题