AngularJS-条件ngStyle [英] AngularJS - conditional ngStyle

查看:113
本文介绍了AngularJS-条件ngStyle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以执行以下操作:

Is there a possibility to do something like this:

<span data-ng-style="vm.myFlag == true ? 'background-color:{{myObject.color}};padding:2px;border-radius:2px;' : ''">

如果-否则不起作用.ngClass不起作用,因为颜色是我对象的属性.

This if - else does not work. ngClass does not work because the color is a property of my object(s).

推荐答案

https://docs.angularjs.org/api/ng/directive/ngStyle

ng-style的基本示例:

Basic Example of ng-style :

<span ng-style="{'background-color':'blue'}">Sample Text</span>

已添加三元运算符,因为这里需要条件样式:

Added ternary operator as conditional styling is expected here:

<span data-ng-style="(vm.myFlag == true) ? {'background-color': myObject.color, 'padding': '2px', 'border-radius': '2px'} : {}">

这篇关于AngularJS-条件ngStyle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆