传递给子指令时,父指令控制器未定义 [英] Parent directive controller undefined when passing to child directive
本文介绍了传递给子指令时,父指令控制器未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这篇文章中询问了一般性问题.我已经给出了工作示例的答案;但是,当我尝试使用此示例修改现有代码时,出现错误. 在下面的插入"页面中查看我的代码.
I asked general question here in this post. I've got answer with working example; however when I try to use this example to modify existing code, I get error. See my code below and in this Plunker page.
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular.min.js"></script>
<div ng-app="myApp">
<tmp-menu ng-disabled="true">
<tmp-menu-link></tmp-menu-link>
<tmp-menu-link></tmp-menu-link>
</tmp-menu>
</div>
JavaScript(AngularJS):
JavaScript(AngularJS):
angular.module('myApp', [])
.controller('MyDirectiveController', MyDirectiveController)
.directive('tmpMenu', function() {
return {
restrict: 'AE',
replace:true,
transclude:true,
scope:{
disabled: '=?ngDisabled'
},
controller: 'MyDirectiveController',
template: '<div>myDirective Disabled: {{ disabled }}<ng-transclude></ng-transclude></div>',
link: function(scope, element, attrs) {
}
};
})
.directive('tmpMenuLink', function() {
return {
restrict: 'AE',
replace:true,
transclude:true,
scope:{
},
required:'^^tmpMenu',
template: '<div>childDirective disabled: {{ disabled }}</div>',
link: function(scope, element, attrs, MyDirectiveCtrl) {
console.log(MyDirectiveCtrl);
scope.disabled = MyDirectiveCtrl.isDisabled();
}
};
})
function MyDirectiveController($scope) {
this.isDisabled = function() {
return $scope.disabled;
};
}
内部指令 tmpMenuLink , MyDirectiveCtrl 是未定义. 为什么会这样?
Inside directive tmpMenuLink, MyDirectiveCtrl is undefined. Why is that?
推荐答案
您的代码中有错字:
required:'^^tmpMenu',
将其更改为
require:'^^tmpMenu',
检查这个笨拙的人
https://plnkr.co/edit/DgyW3OFgr1GyAR8fuATi?p=preview
这篇关于传递给子指令时,父指令控制器未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文