@@ -2790,10 +2790,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
2790
2790
} ;
2791
2791
}
2792
2792
2793
- if ( controllerDirectives ) {
2794
- elementControllers = setupControllers ( $element , attrs , transcludeFn , controllerDirectives , isolateScope , scope , newIsolateScopeDirective ) ;
2795
- }
2796
-
2797
2793
if ( newIsolateScopeDirective ) {
2798
2794
// Initialize isolate scope bindings for new isolate scope directive.
2799
2795
compile . $$addScopeInfo ( $element , isolateScope , true , ! ( templateDirective && ( templateDirective === newIsolateScopeDirective ||
@@ -2809,53 +2805,69 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
2809
2805
}
2810
2806
}
2811
2807
2812
- // Initialize bindToController bindings
2813
- for ( var name in elementControllers ) {
2814
- var controllerDirective = controllerDirectives [ name ] ;
2815
- var controller = elementControllers [ name ] ;
2816
- var bindings = controllerDirective . $$bindings . bindToController ;
2808
+ if ( controllerDirectives ) {
2809
+ elementControllers = createMap ( ) ;
2810
+ for ( var name in controllerDirectives ) {
2811
+ var directive = controllerDirectives [ name ] ;
2812
+ var locals = {
2813
+ $scope : directive === newIsolateScopeDirective || directive . $$isolateScope ? isolateScope : scope ,
2814
+ $element : $element ,
2815
+ $attrs : attrs ,
2816
+ $transclude : transcludeFn
2817
+ } ;
2817
2818
2818
- controller . instance = controller ( ) ;
2819
- $element . data ( '$' + controllerDirective . name + 'Controller' , controller . instance ) ;
2820
- controller . bindingInfo =
2821
- initializeDirectiveBindings ( controllerScope , attrs , controller . instance , bindings , controllerDirective ) ;
2822
- }
2819
+ var controllerConstructor = directive . controller ;
2820
+ if ( controllerConstructor === '@' ) {
2821
+ controllerConstructor = attrs [ name ] ;
2822
+ }
2823
2823
2824
- // Bind the required controllers to the controller, if `require` is an object and `bindToController` is truthy
2825
- forEach ( controllerDirectives , function ( controllerDirective , name ) {
2826
- var require = controllerDirective . require ;
2827
- if ( controllerDirective . bindToController && ! isArray ( require ) && isObject ( require ) ) {
2828
- extend ( elementControllers [ name ] . instance , getControllers ( name , require , $element , elementControllers ) ) ;
2824
+ var instance = $controller ( controllerConstructor , locals , directive . controllerAs ) ;
2825
+
2826
+ $element . data ( '$' + name + 'Controller' , instance ) ;
2827
+
2828
+ // Initialize bindToController bindings
2829
+ var bindings = directive . $$bindings . bindToController ;
2830
+ var bindingInfo = initializeDirectiveBindings ( controllerScope , attrs , instance , bindings , directive ) ;
2831
+
2832
+ elementControllers [ name ] = { instance : instance , bindingInfo : bindingInfo } ;
2829
2833
}
2830
- } ) ;
2831
2834
2832
- // Handle the init and destroy lifecycle hooks on all controllers that have them
2833
- forEach ( elementControllers , function ( controller ) {
2834
- var controllerInstance = controller . instance ;
2835
- if ( isFunction ( controllerInstance . $onChanges ) ) {
2836
- try {
2837
- controllerInstance . $onChanges ( controller . bindingInfo . initialChanges ) ;
2838
- } catch ( e ) {
2839
- $exceptionHandler ( e ) ;
2835
+ // Bind the required controllers to the controller, if `require` is an object and `bindToController` is truthy
2836
+ forEach ( controllerDirectives , function ( controllerDirective , name ) {
2837
+ var require = controllerDirective . require ;
2838
+ if ( controllerDirective . bindToController && ! isArray ( require ) && isObject ( require ) ) {
2839
+ extend ( elementControllers [ name ] . instance , getControllers ( name , require , $element , elementControllers ) ) ;
2840
2840
}
2841
- }
2842
- if ( isFunction ( controllerInstance . $onInit ) ) {
2843
- try {
2844
- controllerInstance . $onInit ( ) ;
2845
- } catch ( e ) {
2846
- $exceptionHandler ( e ) ;
2841
+ } ) ;
2842
+
2843
+ // Handle the init and destroy lifecycle hooks on all controllers that have them
2844
+ forEach ( elementControllers , function ( controller ) {
2845
+ var controllerInstance = controller . instance ;
2846
+ if ( isFunction ( controllerInstance . $onChanges ) ) {
2847
+ try {
2848
+ controllerInstance . $onChanges ( controller . bindingInfo . initialChanges ) ;
2849
+ } catch ( e ) {
2850
+ $exceptionHandler ( e ) ;
2851
+ }
2847
2852
}
2848
- }
2849
- if ( isFunction ( controllerInstance . $doCheck ) ) {
2850
- controllerScope . $watch ( function ( ) { controllerInstance . $doCheck ( ) ; } ) ;
2851
- controllerInstance . $doCheck ( ) ;
2852
- }
2853
- if ( isFunction ( controllerInstance . $onDestroy ) ) {
2854
- controllerScope . $on ( '$destroy' , function callOnDestroyHook ( ) {
2855
- controllerInstance . $onDestroy ( ) ;
2856
- } ) ;
2857
- }
2858
- } ) ;
2853
+ if ( isFunction ( controllerInstance . $onInit ) ) {
2854
+ try {
2855
+ controllerInstance . $onInit ( ) ;
2856
+ } catch ( e ) {
2857
+ $exceptionHandler ( e ) ;
2858
+ }
2859
+ }
2860
+ if ( isFunction ( controllerInstance . $doCheck ) ) {
2861
+ controllerScope . $watch ( function ( ) { controllerInstance . $doCheck ( ) ; } ) ;
2862
+ controllerInstance . $doCheck ( ) ;
2863
+ }
2864
+ if ( isFunction ( controllerInstance . $onDestroy ) ) {
2865
+ controllerScope . $on ( '$destroy' , function callOnDestroyHook ( ) {
2866
+ controllerInstance . $onDestroy ( ) ;
2867
+ } ) ;
2868
+ }
2869
+ } ) ;
2870
+ }
2859
2871
2860
2872
// PRELINKING
2861
2873
for ( i = 0 , ii = preLinkFns . length ; i < ii ; i ++ ) {
@@ -2983,34 +2995,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
2983
2995
return value || null ;
2984
2996
}
2985
2997
2986
- function setupControllers ( $element , attrs , transcludeFn , controllerDirectives , isolateScope , scope , newIsolateScopeDirective ) {
2987
- var elementControllers = createMap ( ) ;
2988
- for ( var controllerKey in controllerDirectives ) {
2989
- var directive = controllerDirectives [ controllerKey ] ;
2990
- var locals = {
2991
- $scope : directive === newIsolateScopeDirective || directive . $$isolateScope ? isolateScope : scope ,
2992
- $element : $element ,
2993
- $attrs : attrs ,
2994
- $transclude : transcludeFn
2995
- } ;
2996
-
2997
- var controller = directive . controller ;
2998
- if ( controller === '@' ) {
2999
- controller = attrs [ directive . name ] ;
3000
- }
3001
-
3002
- var controllerInstance = $controller ( controller , locals , true , directive . controllerAs ) ;
3003
-
3004
- // For directives with element transclusion the element is a comment.
3005
- // In this case .data will not attach any data.
3006
- // Instead, we save the controllers for the element in a local hash and attach to .data
3007
- // later, once we have the actual element.
3008
- elementControllers [ directive . name ] = controllerInstance ;
3009
- $element . data ( '$' + directive . name + 'Controller' , controllerInstance . instance ) ;
3010
- }
3011
- return elementControllers ;
3012
- }
3013
-
3014
2998
// Depending upon the context in which a directive finds itself it might need to have a new isolated
3015
2999
// or child scope created. For instance:
3016
3000
// * if the directive has been pulled into a template because another directive with a higher priority
0 commit comments