@@ -1731,54 +1731,48 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
1731
1731
1732
1732
1733
1733
function getControllers ( directiveName , require , $element , elementControllers ) {
1734
- var value , retrievalMethod = 'data' , optional = false ;
1735
- var $searchElement = $element ;
1736
- var match ;
1737
- if ( isString ( require ) ) {
1738
- match = require . match ( REQUIRE_PREFIX_REGEXP ) ;
1739
- require = require . substring ( match [ 0 ] . length ) ;
1740
-
1741
- if ( match [ 3 ] ) {
1742
- if ( match [ 1 ] ) match [ 3 ] = null ;
1743
- else match [ 1 ] = match [ 3 ] ;
1744
- }
1745
- if ( match [ 1 ] === '^' ) {
1746
- retrievalMethod = 'inheritedData' ;
1747
- } else if ( match [ 1 ] === '^^' ) {
1748
- retrievalMethod = 'inheritedData' ;
1749
- $searchElement = $element . parent ( ) ;
1750
- }
1751
- if ( match [ 2 ] === '?' ) {
1752
- optional = true ;
1753
- }
1734
+ var i , value ;
1754
1735
1755
- value = null ;
1736
+ if ( typeof require === 'string' ) {
1737
+ var match = require . match ( REQUIRE_PREFIX_REGEXP ) ;
1738
+ var name = require . substring ( match [ 0 ] . length ) ;
1739
+ var type = match [ 1 ] || match [ 3 ] ;
1756
1740
1757
- if ( elementControllers && retrievalMethod === 'data' ) {
1758
- if ( value = elementControllers [ require ] ) {
1759
- value = value . instance ;
1760
- }
1741
+ //If only parents then start at the parent element
1742
+ //Otherwise attempt getting the controller from elementControllers to avoid .data
1743
+ if ( type === '^^' ) {
1744
+ $element = $element . parent ( ) ;
1745
+ } else {
1746
+ value = elementControllers && elementControllers [ name ] ;
1747
+ value = value && value . instance ;
1761
1748
}
1762
- value = value || $searchElement [ retrievalMethod ] ( '$' + require + 'Controller' ) ;
1763
1749
1764
- if ( ! value && ! optional ) {
1750
+ if ( ! value ) {
1751
+ var dataName = '$' + name + 'Controller' ;
1752
+ value = type ? $element . inheritedData ( dataName ) : $element . data ( dataName ) ;
1753
+ }
1754
+
1755
+ if ( ! value && match [ 2 ] !== '?' ) {
1765
1756
throw $compileMinErr ( 'ctreq' ,
1766
1757
"Controller '{0}', required by directive '{1}', can't be found!" ,
1767
- require , directiveName ) ;
1758
+ name , directiveName ) ;
1768
1759
}
1760
+
1769
1761
return value || null ;
1770
- } else if ( isArray ( require ) ) {
1771
- value = [ ] ;
1772
- forEach ( require , function getControllersEach ( require ) {
1773
- value . push ( getControllers ( directiveName , require , $element , elementControllers ) ) ;
1774
- } ) ;
1775
1762
}
1776
- return value ;
1763
+
1764
+ if ( isArray ( require ) ) {
1765
+ value = new Array ( i = require . length ) ;
1766
+ while ( i -- ) {
1767
+ value [ i ] = getControllers ( directiveName , require [ i ] , $element , elementControllers ) ;
1768
+ }
1769
+ return value ;
1770
+ }
1777
1771
}
1778
1772
1779
1773
1780
1774
function nodeLinkFn ( childLinkFn , scope , linkNode , $rootElement , boundTranscludeFn ) {
1781
- var i , ii , linkFn , controller , isolateScope , elementControllers , transcludeFn , $element ,
1775
+ var i , ii , linkFn , directive , controller , isolateScope , elementControllers , transcludeFn , $element ,
1782
1776
attrs ;
1783
1777
1784
1778
if ( compileNode === linkNode ) {
@@ -1797,31 +1791,35 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
1797
1791
if ( controllerDirectives ) {
1798
1792
elementControllers = { } ;
1799
1793
1800
- forEach ( controllerDirectives , function nodeLinkControllers ( directive ) {
1794
+ // For directives with element transclusion the element is a comment,
1795
+ // but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1796
+ // clean up (http://bugs.jquery.com/ticket/8335).
1797
+ // Instead, we save the controllers for the element in a local hash and attach to .data
1798
+ // later, once we have the actual element.
1799
+ var controllerData = ! hasElementTranscludeDirective && $element . data ( ) ;
1800
+
1801
+ for ( var directiveName in controllerDirectives ) {
1802
+ var directive = controllerDirectives [ directiveName ] ;
1803
+
1801
1804
var locals = {
1802
1805
$scope : directive === newIsolateScopeDirective || directive . $$isolateScope ? isolateScope : scope ,
1803
1806
$element : $element ,
1804
1807
$attrs : attrs ,
1805
1808
$transclude : transcludeFn
1806
- } , controllerInstance ;
1809
+ } ;
1807
1810
1808
- controller = directive . controller ;
1809
- if ( controller == '@' ) {
1811
+ var controller = directive . controller ;
1812
+ if ( controller === '@' ) {
1810
1813
controller = attrs [ directive . name ] ;
1811
1814
}
1812
1815
1813
- controllerInstance = $controller ( controller , locals , true , directive . controllerAs ) ;
1816
+ var controllerInstance = $controller ( controller , locals , true , directive . controllerAs ) ;
1814
1817
1815
- // For directives with element transclusion the element is a comment,
1816
- // but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1817
- // clean up (http://bugs.jquery.com/ticket/8335).
1818
- // Instead, we save the controllers for the element in a local hash and attach to .data
1819
- // later, once we have the actual element.
1820
1818
elementControllers [ directive . name ] = controllerInstance ;
1821
- if ( ! hasElementTranscludeDirective ) {
1822
- $element . data ( '$' + directive . name + 'Controller' , controllerInstance . instance ) ;
1819
+ if ( controllerData ) {
1820
+ controllerData [ '$' + directive . name + 'Controller' ] = controllerInstance . instance ;
1823
1821
}
1824
- } ) ;
1822
+ }
1825
1823
}
1826
1824
1827
1825
if ( newIsolateScopeDirective ) {
@@ -1908,10 +1906,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
1908
1906
} ) ;
1909
1907
}
1910
1908
1911
- if ( elementControllers ) {
1912
- forEach ( elementControllers , function nodeLinkInitController ( controller ) {
1913
- controller ( ) ;
1914
- } ) ;
1909
+ // Initialize the controllers before linking
1910
+ for ( i in elementControllers ) {
1911
+ elementControllers [ i ] ( ) ;
1915
1912
}
1916
1913
1917
1914
// PRELINKING
0 commit comments