2014-07-24 13 views
22

Questa è una domanda semplice, ma io non riesco a trovare alcuna documentazione relativa ...Una direttiva angolare può richiedere un proprio controllore?

sto cercando di scoprire se una direttiva angolare può sia erediterà un controller genitore così come la sua propria. Prendere in considerazione i seguenti esempi:

Ereditarietà semplice da self

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

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

eredità dal padre

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 
    } 
    } 
}); 

C'è anche l'ereditarietà multipla ...

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` 
    } 
    } 
}); 

Quello che non riesco a capire è come fare in modo che una direttiva erediti sia un controllore genitore che il proprio. In questo modo:

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 
    } 
    } 
}); 

È questo possibile forma/corretta (o è una sorta di anti-modello sono a conoscenza di)?

risposta

34

Beh, si è rivelato molto più ovvio di quanto pensassi ... un po 'di prove ed errori hanno dimostrato che una direttiva può effettivamente richiedere anche se stessa.

Il modo corretto di ereditare controllori genitore + locali sembra essere:

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

questo è il modo in cui sto facendo ora ... il mio SO ricerca speravo per rivelare un metodo più 'elegante' . Immagino che questo sia esattamente come è stato fatto =/ – Roi

+0

Sono così felice che tu abbia fatto questa domanda, stavo passando un bel pò di tempo a capirlo. – richbai90

+0

Perché è necessario che^richieda la direttiva sullo schermo? – Winnemucca

Problemi correlati