2017-07-27 64 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

相關問題