我有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>
我也在该文件中打印$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 :(得分:0)
使用角度指令&#39; s&#39;要求&#39;属性。 在你的指令B中,
return {
restrict: 'E',
templateUrl: 'scripts/modules/slmFlowClass/FlowClass.FlowChart.Template.html',
replace: true,
require: 'A'
....
有关此内容的更多信息,请参阅角度文档以获取指令。 https://docs.angularjs.org/guide/directive