This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +39
-1
lines changed
2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -2007,9 +2007,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
2007
2007
for ( i in elementControllers ) {
2008
2008
controller = elementControllers [ i ] ;
2009
2009
var controllerResult = controller ( ) ;
2010
+
2010
2011
if ( controllerResult !== controller . instance ) {
2012
+ // If the controller constructor has a return value, overwrite the instance
2013
+ // from setupControllers and update the element data
2011
2014
controller . instance = controllerResult ;
2012
- $element . data ( '$' + directive . name + 'Controller' , controllerResult ) ;
2015
+ $element . data ( '$' + i + 'Controller' , controllerResult ) ;
2013
2016
if ( controller === controllerForBindings ) {
2014
2017
// Remove and re-install bindToController bindings
2015
2018
thisLinkFn . $$destroyBindings ( ) ;
Original file line number Diff line number Diff line change @@ -4592,6 +4592,41 @@ describe('$compile', function() {
4592
4592
} ) ;
4593
4593
4594
4594
4595
+ it ( 'should correctly assign controller return values for multiple directives' , function ( ) {
4596
+ var directiveController , otherDirectiveController ;
4597
+ module ( function ( ) {
4598
+
4599
+ directive ( 'myDirective' , function ( log ) {
4600
+ return {
4601
+ scope : true ,
4602
+ controller : function ( $scope ) {
4603
+ return directiveController = {
4604
+ foo : 'bar'
4605
+ } ;
4606
+ }
4607
+ } ;
4608
+ } ) ;
4609
+
4610
+ directive ( 'myOtherDirective' , function ( log ) {
4611
+ return {
4612
+ controller : function ( $scope ) {
4613
+ return otherDirectiveController = {
4614
+ baz : 'luh'
4615
+ } ;
4616
+ }
4617
+ } ;
4618
+ } ) ;
4619
+
4620
+ } ) ;
4621
+
4622
+ inject ( function ( log , $compile , $rootScope ) {
4623
+ element = $compile ( '<my-directive my-other-directive></my-directive>' ) ( $rootScope ) ;
4624
+ expect ( element . data ( '$myDirectiveController' ) ) . toBe ( directiveController ) ;
4625
+ expect ( element . data ( '$myOtherDirectiveController' ) ) . toBe ( otherDirectiveController ) ;
4626
+ } ) ;
4627
+ } ) ;
4628
+
4629
+
4595
4630
it ( 'should get required parent controller' , function ( ) {
4596
4631
module ( function ( ) {
4597
4632
directive ( 'nested' , function ( log ) {
You can’t perform that action at this time.
0 commit comments