Dynamically loading a material theme in angular

前端 未结 6 1917
广开言路
广开言路 2020-12-10 07:55

I\'m building an Angular application using Angular Material. One of the first steps in the application is that the user logs in. Next, the system loads from the backend the

6条回答
  •  有刺的猬
    2020-12-10 08:41

    The following worked for me, all credit to Guilhermecvm, however I'll add the code from the Github issue in case google brings anyone else here

    angular.module('app')
    .config(['$mdThemingProvider', '$provide', function($mdThemingProvider, $provide) {
        $mdThemingProvider.generateThemesOnDemand(true);
        $provide.value('themeProvider', $mdThemingProvider);
    }])
    
    .controller('AppController', ['themeProvider', '$mdTheming', function(themeProvider, $mdTheming) {
        //create new theme
        themeProvider.theme('default')
          .primaryPalette('pink')
          .accentPalette('orange')
          .backgroundPalette('yellow');
    
        //reload the theme
        $mdTheming.generateTheme('default');
    
        //optional - set the default to this new theme
        themeProvider.setDefaultTheme('default');
    }]);
    

    To avoid 'attempted to use unregistered theme' warnings, the theme should be added to the mdTheming internal list of themes

    var theme = themeProvider.theme...
    $mdTheming.THEMES[themeName] = theme;
    

提交回复
热议问题