How can I add additional style properties in CSS in Angular?

∥☆過路亽.° 提交于 2019-12-23 03:58:10

问题


I have a property which should set the font size of a text element. I've tried this so far:

<textarea 
  placeholder="Enter your TAX ID" 
  id="tax" 
  [(ngModel)]="tax_out" 
  [(ngModel)]="tax_in" 
  style="width: 100%; height: 100%; position: relative; bottom: 0px; fontsize: {{fontsize}}">
</textarea>

Unfortunately without success.

I get this in logcat:

"WARNING: sanitizing unsafe style value width: 100%; height: 100%; position: relative; bottom: 0px; fontsize: (see http://g.co/ng/security#xss)."

"[WARN] TypeError: Cannot set property style of [object Object] which has only a getter"

"ERROR", source: ng:///AppModule/ModalContentPage.ngfactory.js (154)

"ERROR CONTEXT", source: ng:///AppModule/ModalContentPage.ngfactory.js (154)


回答1:


If it's just a single style, you can use property binding for this:

<textarea [style.font-size.px]="fontsize" ... style="width: 100%; height: 100%; position: relative; bottom: 0px;"></textarea>

Please take a look at the ngStyle docs for more info if you need to set more than one style dynamically:

<some-element [ngStyle]="{'font-style': styleExp}">...</some-element>    
<some-element [ngStyle]="{'max-width.px': widthExp}">...</some-element>    
<some-element [ngStyle]="objExp">...</some-element>


来源:https://stackoverflow.com/questions/44883775/how-can-i-add-additional-style-properties-in-css-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!