@@ -1787,54 +1787,48 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
1787
1787
1788
1788
1789
1789
function getControllers ( directiveName , require , $element , elementControllers ) {
1790
- var value , retrievalMethod = 'data' , optional = false ;
1791
- var $searchElement = $element ;
1792
- var match ;
1793
- if ( isString ( require ) ) {
1794
- match = require . match ( REQUIRE_PREFIX_REGEXP ) ;
1795
- require = require . substring ( match [ 0 ] . length ) ;
1796
-
1797
- if ( match [ 3 ] ) {
1798
- if ( match [ 1 ] ) match [ 3 ] = null ;
1799
- else match [ 1 ] = match [ 3 ] ;
1800
- }
1801
- if ( match [ 1 ] === '^' ) {
1802
- retrievalMethod = 'inheritedData' ;
1803
- } else if ( match [ 1 ] === '^^' ) {
1804
- retrievalMethod = 'inheritedData' ;
1805
- $searchElement = $element . parent ( ) ;
1806
- }
1807
- if ( match [ 2 ] === '?' ) {
1808
- optional = true ;
1809
- }
1790
+ var i , value ;
1810
1791
1811
- value = null ;
1792
+ if ( typeof require === 'string' ) {
1793
+ var match = require . match ( REQUIRE_PREFIX_REGEXP ) ;
1794
+ var name = require . substring ( match [ 0 ] . length ) ;
1795
+ var type = match [ 1 ] || match [ 3 ] ;
1812
1796
1813
- if ( elementControllers && retrievalMethod === 'data' ) {
1814
- if ( value = elementControllers [ require ] ) {
1815
- value = value . instance ;
1816
- }
1797
+ //If only parents then start at the parent element
1798
+ //Otherwise attempt getting the controller from elementControllers to avoid .data
1799
+ if ( type === '^^' ) {
1800
+ $element = $element . parent ( ) ;
1801
+ } else {
1802
+ value = elementControllers && elementControllers [ name ] ;
1803
+ value = value && value . instance ;
1817
1804
}
1818
- value = value || $searchElement [ retrievalMethod ] ( '$' + require + 'Controller' ) ;
1819
1805
1820
- if ( ! value && ! optional ) {
1806
+ if ( ! value ) {
1807
+ var dataName = '$' + name + 'Controller' ;
1808
+ value = type ? $element . inheritedData ( dataName ) : $element . data ( dataName ) ;
1809
+ }
1810
+
1811
+ if ( ! value && match [ 2 ] !== '?' ) {
1821
1812
throw $compileMinErr ( 'ctreq' ,
1822
1813
"Controller '{0}', required by directive '{1}', can't be found!" ,
1823
- require , directiveName ) ;
1814
+ name , directiveName ) ;
1824
1815
}
1816
+
1825
1817
return value || null ;
1826
- } else if ( isArray ( require ) ) {
1827
- value = [ ] ;
1828
- forEach ( require , function getControllersEach ( require ) {
1829
- value . push ( getControllers ( directiveName , require , $element , elementControllers ) ) ;
1830
- } ) ;
1831
1818
}
1832
- return value ;
1819
+
1820
+ if ( isArray ( require ) ) {
1821
+ value = new Array ( i = require . length ) ;
1822
+ while ( i -- ) {
1823
+ value [ i ] = getControllers ( directiveName , require [ i ] , $element , elementControllers ) ;
1824
+ }
1825
+ return value ;
1826
+ }
1833
1827
}
1834
1828
1835
1829
1836
1830
function nodeLinkFn ( childLinkFn , scope , linkNode , $rootElement , boundTranscludeFn ) {
1837
- var i , ii , linkFn , controller , isolateScope , elementControllers , transcludeFn , $element ,
1831
+ var i , ii , linkFn , directive , controller , isolateScope , elementControllers , transcludeFn , $element ,
1838
1832
attrs ;
1839
1833
1840
1834
if ( compileNode === linkNode ) {
@@ -1859,31 +1853,35 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
1859
1853
if ( controllerDirectives ) {
1860
1854
elementControllers = { } ;
1861
1855
1862
- forEach ( controllerDirectives , function nodeLinkControllers ( directive ) {
1856
+ // For directives with element transclusion the element is a comment,
1857
+ // but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1858
+ // clean up (http://bugs.jquery.com/ticket/8335).
1859
+ // Instead, we save the controllers for the element in a local hash and attach to .data
1860
+ // later, once we have the actual element.
1861
+ var controllerData = ! hasElementTranscludeDirective && $element . data ( ) ;
1862
+
1863
+ for ( var directiveName in controllerDirectives ) {
1864
+ var directive = controllerDirectives [ directiveName ] ;
1865
+
1863
1866
var locals = {
1864
1867
$scope : directive === newIsolateScopeDirective || directive . $$isolateScope ? isolateScope : scope ,
1865
1868
$element : $element ,
1866
1869
$attrs : attrs ,
1867
1870
$transclude : transcludeFn
1868
- } , controllerInstance ;
1871
+ } ;
1869
1872
1870
- controller = directive . controller ;
1871
- if ( controller == '@' ) {
1873
+ var controller = directive . controller ;
1874
+ if ( controller === '@' ) {
1872
1875
controller = attrs [ directive . name ] ;
1873
1876
}
1874
1877
1875
- controllerInstance = $controller ( controller , locals , true , directive . controllerAs ) ;
1878
+ var controllerInstance = $controller ( controller , locals , true , directive . controllerAs ) ;
1876
1879
1877
- // For directives with element transclusion the element is a comment,
1878
- // but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1879
- // clean up (http://bugs.jquery.com/ticket/8335).
1880
- // Instead, we save the controllers for the element in a local hash and attach to .data
1881
- // later, once we have the actual element.
1882
1880
elementControllers [ directive . name ] = controllerInstance ;
1883
- if ( ! hasElementTranscludeDirective ) {
1884
- $element . data ( '$' + directive . name + 'Controller' , controllerInstance . instance ) ;
1881
+ if ( controllerData ) {
1882
+ controllerData [ '$' + directive . name + 'Controller' ] = controllerInstance . instance ;
1885
1883
}
1886
- } ) ;
1884
+ }
1887
1885
}
1888
1886
1889
1887
if ( newIsolateScopeDirective ) {
@@ -1970,10 +1968,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
1970
1968
} ) ;
1971
1969
}
1972
1970
1973
- if ( elementControllers ) {
1974
- forEach ( elementControllers , function nodeLinkInitController ( controller ) {
1975
- controller ( ) ;
1976
- } ) ;
1971
+ // Initialize the controllers before linking
1972
+ for ( i in elementControllers ) {
1973
+ elementControllers [ i ] ( ) ;
1977
1974
}
1978
1975
1979
1976
// PRELINKING
0 commit comments