angularmoduleSiemensAppSYSUIPmWorkstationDataConfigconfigListScreenRouteConfig; 如何获取子directive
要获取子directive,可以使用指令的controller中的$children属性。$children是一个数组,包含所有子directive的controller。例如:
angular.module('Siemens.AppSYS.UIPmWorkstationDataConfig')
.directive('parentDirective', function() {
return {
restrict: 'E',
controller: function() {
var vm = this;
vm.children = [];
this.addChild = function(child) {
vm.children.push(child);
};
},
link: function(scope, element, attrs, parentCtrl) {
// 在link函数中获取所有子directive的controller
var childrenCtrl = parentCtrl.$children;
}
};
})
.directive('childDirective', function() {
return {
restrict: 'E',
require: '^parentDirective',
link: function(scope, element, attrs, parentCtrl) {
// 将自己的controller添加到父directive的$children数组中
parentCtrl.addChild(this);
}
};
});
在上面的代码中,parentDirective是父directive,childDirective是子directive。在parentDirective的controller中定义了一个$children数组,并提供了一个addChild方法用于将子directive的controller添加到$children数组中。在childDirective的link函数中,调用父directive的addChild方法将自己的controller添加到$children数组中。这样,就可以在父directive的controller中获取到所有子directive的controller了
原文地址: https://www.cveoy.top/t/topic/huBt 著作权归作者所有。请勿转载和采集!