angular.js - $mdThemingProvider 不能在controller中使用吗
高洛峰
高洛峰 2017-05-15 16:56:08
0
2
748

.config(CoreTheme)

function CoreTheme($mdThemingProvider) {
    $mdThemingProvider.theme('default')
        .primaryPalette('blue',{
            'default'   :   '500',
            'hue-1'     :   '900',
            'hue-2'     :   'A100',
            'hue-3'     :   '400'
        })
        .accentPalette('green',{
            'default'   :   '600',
            'hue-1'     :   '900',
            'hue-2'     :   'A100',
            'hue-3'     :   '400'
        })
        .warnPalette('deep-orange',{
            'default'   :   '500',
        });
    $mdThemingProvider.alwaysWatchTheme(true)
}

CoreTheme.$inject = ['$mdThemingProvider'];

export default CoreTheme;

module.js

export default angular.module('theme',['ngMaterial']);

controller.js

class ThemeCtrl{
    constructor($scope,$rootScope,$mdThemingProvider){
   
        $scope.changeTheme = changeTheme;


        function changeTheme(theme_name){
            $mdThemingProvider.theme('default').dark()
        }
    }
}

ThemeCtrl.$inject = ['$scope','$rootScope','$mdThemingProvider'];

export default ThemeCtrl;

我想在这个控制器中 动态改变主题样式 可是注入这个Provider报错!应该怎么做?
我能想到的是(1)可以在.config()中$watch某个变量 来改变主题吗?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
给我你的怀抱

Reference question
I just looked at the latest material source code. It seems that there is no reload function. If you want to use the following method, you still need to modify the material source code. If you do not change the source code, please ignore this answer.

You can try it as follows:
1. Modify the source code of material according to the answers in the reference questions

2. In the configure phase, register $mdThemingProvider for controller use

angular.module("yourModule",['whateverDependencies']).config(
function($provide, $mdThemingProvider) { 
    $provide.value('themeProvider', $mdThemingProvider); 
})

3. Reload the theme in the controller

.controller('someController', function(themeProvider, $injector) {         
  themeProvider.theme('default').dark();
  themeProvider.reload($injector);
}
曾经蜡笔没有小新

Configuring of the default theme is done by using the $mdThemingProvider during application configuration.
The official website said, please configure it in configuration.

angular material has already given a theme when generating the page. For example, after md-button is generated, it is md-button md-default-theme. If you want to modify the theme, you can traverse the page and replace md-default-theme with the theme you want, md-dark-theme.

But it’s tiring to do this, right?

So it is best to open a preview in a new window after selecting a theme.
Or have a separate theme page to list what all themes look like. Just have options. After all, previewing the interface on the original website is a very annoying thing.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!