如何获得通过功能NG风格的多CSS规则?如何获得、规则、风格、功能

2023-09-13 04:35:20 作者:♀浅殇

我需要申请多的CSS规则HTML标签中的角表单模板。

I need for apply multi css rule to html tag in angular form template.

<div class="form-control" id="data.objectStyle"  
  ng-model="data.type" 
  ng-style="getStyle(data.objectStyle)">
{{data.objectStyle.title}}
</div>

在控制器的getStyle功能:

getStyle function in controller :

$scope.getStyle = function (taskType) {
    return {
         background-color:taskType.backColor,
         color: taskType.color,
         font-size:taskType.fontSize,
         font-family:taskType.font
    }
)};

任务类型的对象:

taskType object:

{
    backColor:'#006',
    color:'#56DA',
    fontSize:12,
    font:'New Times Roman'
}

的getStyle 函数不返回一个风格!怎么办?

The getStyle function does not return a style! What to do?

推荐答案

借助文档指定你需要用你的钥匙放在引号如此他们不是无效的JSON对象键:

EDIT

The docs specify that you need to wrap your keys in quotation marks so they aren't invalid JSON object keys:

return {
     "background-color": taskType.backColor,
     "color": taskType.color,
     "font-size":taskType.fontSize,
     "font-family":taskType.font
}

旧的答案(不使用NG式)

虽然我从来没有使用过 NG式,它似乎并没有采取的对象。相反,它是纳克级的,但单风格等效。

Old Answer (not using ng-style)

While I never used ng-style, it doesn't seem to take objects. Rather it is an equivalent of ng-class but for single styles.

试着改变你的功能:

$scope.getStyle = function (taskType) {

        return {
         "background-color:"+taskType.backColor+
         ";color:"+ taskType.color+
         ";font-size:"+taskType.fontSize+
         ";font-family:"+taskType.font+";";
    }
)};

和使用常规的风格标签用绑定在html:

and the html to use the regular style tag with a bind:

<div class="form-control" id="data.objectStyle"  ng-model="data.type" style="{{getStyle(data.objectStyle)}}">