Angular2 dynamic change CSS property

前端 未结 5 1372
粉色の甜心
粉色の甜心 2020-12-01 02:07

We are making an Angular2 application and we want to be able to somehow create a global CSS variable (and update the properties\' values whenever changed wh

5条回答
  •  心在旅途
    2020-12-01 02:28

    You don't have any example code but I assume you want to do something like this?

    @View({
    directives: [NgClass],
    styles: [`
        .${TodoModel.COMPLETED}  {
            text-decoration: line-through;
        }
        .${TodoModel.STARTED} {
            color: green;
        }
    `],
    template: `
    {{todo.title}}
    ` })

    You assign ng-class to a variable which is dynamic (a property of a model called TodoModel as you can guess).

    todo.toggle() is changing the value of todo.status and there for the class of the input is changing.

    This is an example for class name but actually you could do the same think for css properties.

    I hope this is what you meant.

    This example is taken for the great egghead tutorial here.

提交回复
热议问题