Int*_*lix 4 javascript angularjs
我创建了2个指令:Parent和Child.试图让他们互相交谈.看起来如果他们都有范围他们没有收到事件.根据文件说明这似乎不正确$emit:Dispatches an event name upwards through the scope hierarchy notifying the registered ng.$rootScope.Scope#methods_$on listeners.
那么肯定应该通过范围冒泡?
Isolate scope is now available only to the isolate directive that requested it and its template.
这里有一个plunker:http://plnkr.co/edit/EfKJthkQLitWnF2srykM?p=preview
您可以将子指令创建为父指令的模板,因为模板指令确实获得了如上所述的指令范围.
.directive('parent', function ($timeout) {
return {
template:'<child name="Jimmy"></child>',
Run Code Online (Sandbox Code Playgroud)
另一种解决方案是在父指令上创建一个控制器,并在子指令中要求它.
.directive('parent', function ($timeout) {
return {
controller: function(){
this.hungry = function(message){
// something
}
}
Run Code Online (Sandbox Code Playgroud)
儿童指令:
.directive('child', function ($timeout) {
return {
require: "^parent",
link: function (scope, element, attrs, parentCtrl) {
parentCtrl.hungry("I'm hungry!")
Run Code Online (Sandbox Code Playgroud)