跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

Migration from NgStyle to style bindings ​

This schematic migrates NgStyle directive usages to style bindings in your application. It will only migrate usages that are considered safe to migrate.

Run the schematic using the following command:

bash
ng generate @angular/core:ngstyle-to-style

An object with a single key becomes a binding to that one style property.

Before ​

html
<div [ngStyle]="{'background-color': 'red'}"></div>

After ​

html
<div [style.background-color]="'red'"></div>

An object with more than one key becomes a single [style] binding.

Before ​

html
<div [ngStyle]="{'color': 'blue', 'font-weight': 'bold'}"></div>

After ​

html
<div [style]="{'color': 'blue', 'font-weight': 'bold'}"></div>

Configuration options ​

The migration supports a few options for fine tuning the migration to your specific needs.

--best-effort-mode ​

By default, the migration avoids migrating object references usages of NgStyle When the --best-effort-mode flag is enabled, ngStyle instances binded to object references are also migrated. This can be unsafe to migrate, for example if the binded object is mutated.

html
<div [ngStyle]="styleObject"></div>

to

html
<div [style]="styleObject"></div>

基于 MIT 许可证发布。