typescript 无法绑定到“ngClass”,因为它不是“button”的已知属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40902835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:03:07  来源:igfitidea点击:

Can't bind to 'ngClass' since it isn't a known property of 'button'

angulartypescript

提问by BentOnCoding

HTML:

HTML:

<button class="sa-button" [ngClass]="'buttonClass'">{{displayText}}</button>

The variable I expect to bind to in my typescript file:

我希望在打字稿文件中绑定的变量:

public buttonClass = "button";

The error I get:

我得到的错误:

Can't bind to 'ngClass' since it isn't a known property of 'button'

无法绑定到“ngClass”,因为它不是“button”的已知属性

I want to bind a variable which holds the name of the css class I want to apply.

我想绑定一个变量,该变量包含我要应用的 css 类的名称。

Is there a correct way to do this? I literally copied this out of the angular docs, and it doesn't work.

有没有正确的方法来做到这一点?我真的从 angular 文档中复制了这个,但它不起作用。

回答by Chris Noffke

by adding the single quotes, you are passing in the text in the single quotes.

通过添加单引号,您将在单引号中传递文本。

You want to remove them like so...

你想像这样删除它们......

<button class="sa-button" [ngClass]="buttonClass"  >{{displayText}}</button>

Fro more on ngClass, check out the docs

有关 ngClass 的更多信息,请查看文档