Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set text color in Angular-Material?

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?

like image 335
Maor Avatar asked Feb 12 '15 20:02

Maor


3 Answers

I was just able to do this using Angular Material 1.1.0.

You use the md-colors directive similar to how you would ng-class:

<span md-colors="{color:'primary'}">...</span>

The above code will color the text the primary color. The object that you pass to md-colors uses the key as the css property (i.e. 'color', 'background', etc) and the value as the theme and/or palette and/or hue you want to use.

Source Docs

like image 66
coblr Avatar answered Oct 15 '22 08:10

coblr


Angular Material's docs explicitly enumerate list of components, which you able to differentiate with md-theme attribute:

md-button
md-checkbox
md-progress-circular
md-progress-linear
md-radio-button
md-slider
md-switch
md-tabs
md-text-float
md-toolbar

From Angular Material documentation, under Theming / Declarative Syntax.

So I think the short answer is: you can't do it.

like image 33
Zhorzh Alexandr Avatar answered Oct 15 '22 06:10

Zhorzh Alexandr


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 :)

like image 6
Hsin-Yu Chen Avatar answered Oct 15 '22 07:10

Hsin-Yu Chen