2014-07-24 7 views
22

Dies ist eine einfache Frage, aber ich kann nicht sämtliche Dokumente zu finden scheinen ...Kann eine Winkelrichtlinie einen eigenen Controller erfordern?

Ich versuche, wenn eine Winkel Richtlinie sowohl als auch eine übergeordnete Steuerung übernehmen kann, um herauszufinden, wie seine eigene. Betrachten Sie die folgenden Beispiele:

Einfache Vererbung von Selbst

app.directive('screen', function() { 
    return { 
    scope: true, 
    controller: function() { 
     this.doSomething = function() { 

     }; 
    }, 
    link: function($scope, el, attrs, ctrl) { 
     // ctrl now contains `doSomething` 
    } 
    } 
}); 

Vererbung von Eltern

app.directive('screen', function() { 
    return { 
    scope: true, 
    controller: function() { 
     this.doSomething = function() { 

     }; 
    } 
    } 
}); 
app.directive('widget', function() { 
    return { 
    scope: true, 
    require: '^screen', 
    link: function($scope, el, attrs, ctrl) { 
     // ctrl now contains `doSomething` -- inherited from the `screen` directive 
    } 
    } 
}); 

Es gibt sogar Mehrfachvererbung ...

app.directive('screen', function() { 
    return { 
    scope: true, 
    controller: function() { 
     this.doSomething = function() { 

     }; 
    } 
    } 
}); 
app.directive('widget', function() { 
    return { 
    scope: true, 
    require: ['^screen','^anotherParent'], 
    link: function($scope, el, attrs, ctrl) { 
     // ctrl[0] now contains `doSomething` -- inherited from the `screen` directive 
     // ctrl[1] now contains the controller inherited from `anotherParent` 
    } 
    } 
}); 

Was ich nicht herausfinden kann ist, wie man eine Direktive erben kann, die sowohl einen übergeordneten Controller als auch einen eigenen erbt. Wie so:

app.directive('screen', function() { 
    return { 
    scope: true, 
    controller: function() { 
     this.doSomething = function() { 

     }; 
    } 
    } 
}); 
app.directive('widget', function() { 
    return { 
    scope: true, 
    require: '^screen', 
    controller: function($scope) { 
     // isolated widget controller 
    }, 
    link: function($scope, el, attrs, ctrl) { 
     // I need the `screen` controller in ADDITION to the isolated widget controller accessible in the link 
    } 
    } 
}); 

Ist das möglich/die richtige Form (oder ist es eine Art von anti-Muster Ich bin nicht bewusst)?

Antwort

34

Nun, das stellte sich als viel offensichtlicher heraus, als ich dachte ... ein wenig Versuch und Irrtum zeigte, dass eine Direktive sich auch selbst benötigen kann.

Der richtige Weg, Eltern + lokale Steuerungen zu erben scheint zu sein:

app.directive('screen', function() { 
    return { 
    scope: true, 
    controller: function() { 
     this.doSomething = function() { 

     }; 
    } 
    } 
}); 
app.directive('widget', function() { 
    return { 
    scope: true, 
    require: ['^screen','widget'], 
    controller: function($scope) { 
     this.widgetDoSomething = function() { 
     }; 
    }, 
    link: function($scope, el, attrs, ctrl) { 
     // ctrl[0] contains the `screen` controller 
     // ctrl[1] contains the local `widget` controller 
    } 
    } 
}); 
+0

das ist die Art, wie ich es jetzt tue, ... meine SO ein ‚elegant‘ Suche hat gehofft, Verfahren zu offenbaren . Ich denke, das ist genau, wie es gemacht wird =/ – Roi

+0

Ich bin so froh, dass Sie diese Frage gestellt haben, ich hatte eine Menge Zeit damit, es herauszufinden. – richbai90

+0

Warum brauchen Sie^um die Bildschirm-Anweisung zu verlangen? – Winnemucca

Verwandte Themen