儿童指令与母指令
下面下面的代码完美地工作,进行通信时除外我去掉在父指令模板线(parentD)AngularJS:经由控制器API以外的其它指令指令连通
.directive('parentD', ['$window', function($window) {
return {
restrict: 'E',
scope: {},
controller: function($scope) {
this.testvar = 'Hello';
this.doSomething = function() {
$window.alert("This is an alert from the parent");
return this.testvar;
}
},
//template: '<h1>Parent Template</h1>'
}
}])
.directive('childD', ['$window', function($window) {
return {
restrict: 'E',
require: '^parentD',
scope: {},
template: '<h2>Child Template</h2>',
link: function(scope, element, attribute, controller) {
$window.alert('The parent passes this message ' + controller.doSomething());
}
}
这条线未注释时似乎不执行子指令。
http://plnkr.co/edit/h3bMe5mJ0QnbRHIla8l9
感谢您的帮助,我敢肯定,我犯了一个错误的地方,我只需要一组额外的它的眼睛。
需要使用“this”关键字如在我的答案。 – micronyks
感谢您的回复,您的代码看起来与我上面发布的代码完全相同,您能告诉我区别在哪里吗?您是否取消了父级模板行的注释?我认为可能对我最近的抢注没有保存造成混乱,我已经更新了它以反映我原始帖子中的代码。 –
http://plnkr.co/edit/pIkN5Uc3JYQpsbKOMcY1?p=preview请检查这个链接和我的整个更新的答案。你将拥有你正在寻找的工作模板和所有消息。 – micronyks