调用从孩子指令的功能和在父指令
问题描述:
采用可变我有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
为空?
答
使用角度指令的'require'属性。 在你的指令B,
return {
restrict: 'E',
templateUrl: 'scripts/modules/slmFlowClass/FlowClass.FlowChart.Template.html',
replace: true,
require: 'A'
....
有关此的详细信息,请参阅角文档指令。 https://docs.angularjs.org/guide/directive