How to set text color in Angular-Material?

2019-02-05 11:15发布

I want to set one word in a sentence to have md-primary color, and another word to have the accent color. I assumed something like this:

<div>
    Hello <span class="md-primary">friend</span>. 
    How are <span class="md-accent">you</span>?
</div>

But those classes work only for some specified components.

What's the way to do it?

9条回答
SAY GOODBYE
2楼-- · 2019-02-05 11:43

EDITED 2015/07/23

TitForTat's comment has better solution https://github.com/angular/material/issues/1269#issuecomment-121303016


I created a module:

(function () {
        "use strict";
        angular
            .module('custiom.material', ['material.core'])
            .directive('cMdThemeFg', ["$mdTheming", function ($mdTheming) {
                return {
                    restrict: 'A',
                    link: postLink
                };
                function postLink(scope, element, attr) {
                    $mdTheming(element);
                    element.addClass('c-md-fg');
                }

            }])
            .directive('cMdThemeBg', ["$mdTheming", function ($mdTheming) {
                return {
                    restrict: 'A',
                    link: postLink
                };
                function postLink(scope, element, attr) {
                    $mdTheming(element);
                    element.addClass('c-md-bg');
                }
            }]);
    })();

and then append

.c-md-fg.md-THEME_NAME-theme.md-primary {    color: '{{primary-color}}'; }
.c-md-fg.md-THEME_NAME-theme.md-accent {    color: '{{accent-color}}'; } 
.c-md-fg.md-THEME_NAME-theme.md-warn {    color: '{{warn-color}}'; } 
.c-md-bg.md-THEME_NAME-theme.md-primary {    background-color: '{{primary-color}}'; } 
.c-md-bg.md-THEME_NAME-theme.md-accent {    background-color: '{{accent-color}}'; } 
.c-md-bg.md-THEME_NAME-theme.md-warn {    background-color: '{{warn-color}}'; }

into angular-material.js at 13783 line

angular.module("material.core").constant("......  HERE")

Then, I can simply apply c-md-theme-fg and/or c-md-theme-bg to element to apply theme colors. Like this:

<div c-md-theme-bg class="md-primary md-hue-3">dasdasdasd</div>
<span c-md-theme-bg class="md-primary">test</span>

It works.

ps: sorry about english, come from Taiwan :)

查看更多
祖国的老花朵
3楼-- · 2019-02-05 11:43

This answer is outdated please check @factalspawn answer

With 'pure' angular-material you can't. But you can try usign this: https://gist.github.com/senthilprabhut/dd2147ebabc89bf223e7

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-02-05 11:52

Anchor element () is reacting to the Angular Material current theme setting since version 1.0.0-rc1: https://github.com/angular/material/blob/master/CHANGELOG.md#110-rc1-2016-03-09.

查看更多
登录 后发表回答