Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
906 views
in Technique[技术] by (71.8m points)

angularjs - Dynamically loading a material theme in angular

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 theme made by that user's company.

However, when the backend responds with the Theme information, I am not able to get it into the system. The $mdThemingProvider, which allows you to set themes, does not seem to be available from inside a Controller and the $mdTheming service itself does not have options for modifying the themes in the system.

Is there any way to dynamically load new themes into an Angular Material application?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As of version 1.0.5 of angular-material:

  1. Add

    reload: generateTheme 
    

    in themingProvider function (angular-material.js line 4840 - the generateTheme function is already present in angular-material.js, we just need a way to call it)

  2. In config function:

    angular.module('my-module').config(function ($provide, $mdThemingProvider) {
     $mdThemingProvider.generateThemesOnDemand(true);
     $provide.value('themeProvider', $mdThemingProvider);});
    
  3. In your controller, inject themeProvider and now you can do something like:

    //create new theme
    themeProvider.theme('custom')
      .primaryPalette('pink')
      .accentPalette('orange')
      .backgroundPalette('yellow');
    
    //reload the theme
    themeProvider.reload('custom');
    
    //optional - set the default to this new theme
    themeProvider.setDefaultTheme('custom');
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...