@@ -744,6 +744,7 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
744
744
*/
745
745
function inheritTheme ( el , parent ) {
746
746
var ctrl = parent . controller ( 'mdTheme' ) || el . data ( '$mdThemeController' ) ;
747
+ var scope = el . scope ( ) ;
747
748
748
749
updateThemeClass ( lookupThemeName ( ) ) ;
749
750
@@ -752,24 +753,36 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
752
753
ctrl . $shouldWatch ||
753
754
$mdUtil . parseAttributeBoolean ( el . attr ( 'md-theme-watch' ) ) ;
754
755
755
- var unwatch = ctrl . registerChanges ( function ( name ) {
756
- updateThemeClass ( name ) ;
756
+ if ( watchTheme || ctrl . isAsyncTheme ) {
757
+ var clearNameWatcher = function ( ) {
758
+ if ( unwatch ) {
759
+ unwatch ( ) ;
760
+ unwatch = undefined ;
761
+ }
762
+ } ;
757
763
758
- if ( ! watchTheme ) {
759
- unwatch ( ) ;
760
- }
761
- else {
762
- el . on ( '$destroy' , unwatch ) ;
764
+ var unwatch = ctrl . registerChanges ( function ( name ) {
765
+ updateThemeClass ( name ) ;
766
+
767
+ if ( ! watchTheme ) {
768
+ clearNameWatcher ( ) ;
769
+ }
770
+ } ) ;
771
+
772
+ if ( scope ) {
773
+ scope . $on ( '$destroy' , clearNameWatcher ) ;
774
+ } else {
775
+ el . on ( '$destroy' , clearNameWatcher ) ;
763
776
}
764
- } ) ;
777
+ }
765
778
}
766
779
767
780
/**
768
781
* Find the theme name from the parent controller or element data
769
782
*/
770
783
function lookupThemeName ( ) {
771
784
// As a few components (dialog) add their controllers later, we should also watch for a controller init.
772
- return ctrl && ctrl . $mdTheme || ( defaultTheme == 'default' ? '' : defaultTheme ) ;
785
+ return ctrl && ctrl . $mdTheme || ( defaultTheme === 'default' ? '' : defaultTheme ) ;
773
786
}
774
787
775
788
/**
@@ -819,7 +832,13 @@ function ThemingDirective($mdTheming, $interpolate, $parse, $mdUtil, $q, $log) {
819
832
. trim ( )
820
833
. substr ( 0 , oneTimeOperator . length ) === oneTimeOperator ;
821
834
835
+ var getTheme = function ( ) {
836
+ var interpolation = $interpolate ( attrs . mdTheme ) ( scope ) ;
837
+ return $parse ( interpolation ) ( scope ) || interpolation ;
838
+ } ;
839
+
822
840
var ctrl = {
841
+ isAsyncTheme : angular . isFunction ( getTheme ( ) ) || angular . isFunction ( getTheme ( ) . then ) ,
823
842
registerChanges : function ( cb , context ) {
824
843
if ( context ) {
825
844
cb = angular . bind ( context , cb ) ;
@@ -844,7 +863,8 @@ function ThemingDirective($mdTheming, $interpolate, $parse, $mdUtil, $q, $log) {
844
863
845
864
// Iterating backwards to support unregistering during iteration
846
865
// http://stackoverflow.com/a/9882349/890293
847
- // we don't use `reverse()` of array because it mutates the array and we don't want it to get re-indexed
866
+ // we don't use `reverse()` of array because it mutates the array and we don't want it
867
+ // to get re-indexed
848
868
for ( var i = registeredCallbacks . length ; i -- ; ) {
849
869
registeredCallbacks [ i ] ( theme ) ;
850
870
}
@@ -856,18 +876,13 @@ function ThemingDirective($mdTheming, $interpolate, $parse, $mdUtil, $q, $log) {
856
876
857
877
el . data ( '$mdThemeController' , ctrl ) ;
858
878
859
- var getTheme = function ( ) {
860
- var interpolation = $interpolate ( attrs . mdTheme ) ( scope ) ;
861
- return $parse ( interpolation ) ( scope ) || interpolation ;
862
- } ;
863
-
864
879
var setParsedTheme = function ( theme ) {
865
880
if ( typeof theme === 'string' ) {
866
881
return ctrl . $setTheme ( theme ) ;
867
882
}
868
883
869
884
$q . when ( angular . isFunction ( theme ) ? theme ( ) : theme )
870
- . then ( function ( name ) {
885
+ . then ( function ( name ) {
871
886
ctrl . $setTheme ( name ) ;
872
887
} ) ;
873
888
} ;
0 commit comments