@@ -1671,7 +1671,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
1671
1671
1672
1672
if ( ! directive . templateUrl && directive . controller ) {
1673
1673
directiveValue = directive . controller ;
1674
- controllerDirectives = controllerDirectives || { } ;
1674
+ controllerDirectives = controllerDirectives || createMap ( ) ;
1675
1675
assertNoDuplicate ( "'" + directiveName + "' controller" ,
1676
1676
controllerDirectives [ directiveName ] , directive , $compileNode ) ;
1677
1677
controllerDirectives [ directiveName ] = directive ;
@@ -1877,6 +1877,36 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
1877
1877
return value || null ;
1878
1878
}
1879
1879
1880
+ function setupControllers ( $element , attrs , transcludeFn , controllerDirectives , isolateScope , scope ) {
1881
+ var elementControllers = createMap ( ) ;
1882
+ for ( var controllerKey in controllerDirectives ) {
1883
+ var directive = controllerDirectives [ controllerKey ] ;
1884
+ var locals = {
1885
+ $scope : directive === newIsolateScopeDirective || directive . $$isolateScope ? isolateScope : scope ,
1886
+ $element : $element ,
1887
+ $attrs : attrs ,
1888
+ $transclude : transcludeFn
1889
+ } ;
1890
+
1891
+ var controller = directive . controller ;
1892
+ if ( controller == '@' ) {
1893
+ controller = attrs [ directive . name ] ;
1894
+ }
1895
+
1896
+ var controllerInstance = $controller ( controller , locals , true , directive . controllerAs ) ;
1897
+
1898
+ // For directives with element transclusion the element is a comment,
1899
+ // but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1900
+ // clean up (http://bugs.jquery.com/ticket/8335).
1901
+ // Instead, we save the controllers for the element in a local hash and attach to .data
1902
+ // later, once we have the actual element.
1903
+ elementControllers [ directive . name ] = controllerInstance ;
1904
+ if ( ! hasElementTranscludeDirective ) {
1905
+ $element . data ( '$' + directive . name + 'Controller' , controllerInstance . instance ) ;
1906
+ }
1907
+ }
1908
+ return elementControllers ;
1909
+ }
1880
1910
1881
1911
function nodeLinkFn ( childLinkFn , scope , linkNode , $rootElement , boundTranscludeFn ,
1882
1912
thisLinkFn ) {
@@ -1903,32 +1933,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
1903
1933
}
1904
1934
1905
1935
if ( controllerDirectives ) {
1906
- elementControllers = { } ;
1907
- forEach ( controllerDirectives , function ( directive ) {
1908
- var locals = {
1909
- $scope : directive === newIsolateScopeDirective || directive . $$isolateScope ? isolateScope : scope ,
1910
- $element : $element ,
1911
- $attrs : attrs ,
1912
- $transclude : transcludeFn
1913
- } , controllerInstance ;
1914
-
1915
- controller = directive . controller ;
1916
- if ( controller == '@' ) {
1917
- controller = attrs [ directive . name ] ;
1918
- }
1919
-
1920
- controllerInstance = $controller ( controller , locals , true , directive . controllerAs ) ;
1921
-
1922
- // For directives with element transclusion the element is a comment,
1923
- // but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1924
- // clean up (http://bugs.jquery.com/ticket/8335).
1925
- // Instead, we save the controllers for the element in a local hash and attach to .data
1926
- // later, once we have the actual element.
1927
- elementControllers [ directive . name ] = controllerInstance ;
1928
- if ( ! hasElementTranscludeDirective ) {
1929
- $element . data ( '$' + directive . name + 'Controller' , controllerInstance . instance ) ;
1930
- }
1931
- } ) ;
1936
+ elementControllers = setupControllers ( $element , attrs , transcludeFn , controllerDirectives , isolateScope , scope ) ;
1932
1937
}
1933
1938
1934
1939
if ( newIsolateScopeDirective ) {
@@ -1958,17 +1963,18 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
1958
1963
bindings , scopeDirective ) ;
1959
1964
}
1960
1965
}
1961
- forEach ( elementControllers , function ( controller ) {
1962
- var result = controller ( ) ;
1963
- if ( result !== controller . instance &&
1966
+ for ( i in elementControllers ) {
1967
+ controller = elementControllers [ i ] ;
1968
+ var controllerResult = controller ( ) ;
1969
+ if ( controllerResult !== controller . instance &&
1964
1970
controller === controllerForBindings ) {
1965
1971
// Remove and re-install bindToController bindings
1966
1972
thisLinkFn . $$destroyBindings ( ) ;
1967
1973
thisLinkFn . $$destroyBindings =
1968
- initializeDirectiveBindings ( scope , attrs , result ,
1974
+ initializeDirectiveBindings ( scope , attrs , controllerResult ,
1969
1975
bindings , scopeDirective ) ;
1970
1976
}
1971
- } ) ;
1977
+ }
1972
1978
}
1973
1979
1974
1980
// PRELINKING
0 commit comments