Angular 8 中 ng-attr-style 的等价物是什么?

What is the equivalent of ng-attr-style in Angular 8?

我在 AngularJS 中有一个应用程序,我想将其转换为 Angular8.

在应用程序中,有一个 div,我在其中动态传递宽度,如下所示:

ng-attr-style="width:{{countersDtl.SurveySubmittedInstant/(countersDtl.ServiceDone)*100}}%"

countersDtl 是一个具有键 SurveySubmittedInstantServiceDone 的对象。

我有多个元素正在使用 ng-attr-style 就像这样。

我已经尝试了从 style.widthngStyle 的所有方法,但没有任何效果。

ng-attr-style 等同于 ngStyle 绑定。 您可以在 docs.

中阅读有关 ngStyle 的更多信息

您的案例的正确语法可能如下所示:

[ngStyle]="{'width': (countersDtl.SurveySubmittedInstant/(countersDtl.ServiceDone)*100) + '%'"

此外,您可以使用而不是 + '%' 将 属性 标记为 'width.%',如下所示:

[ngStyle]="{'width.%': countersDtl.SurveySubmittedInstant/(countersDtl.ServiceDone)*100"

还有第三种可能绑定到 width 本身,像这样:

[style.width.%]="countersDtl.SurveySubmittedInstant/(countersDtl.ServiceDone)*100"