Angularjs 使用ng样式和不同条件更改颜色

Angularjs 使用ng样式和不同条件更改颜色,angularjs,Angularjs,我想改变我的td的颜色参数,所以我尝试了这个,效果很好: {{item.name} 但是当我尝试使用不同的条件,例如 item.name:xxx=>颜色:红色 item.name:yyy=>颜色:黄色 item.name:aaa=>color:blue您可以有条件地应用类: <td ng-class="{red: item.name === 'xxx', yellow: item.name === 'yyy'}"> {{ item.name }} </td> C

我想改变我的td的颜色参数,所以我尝试了这个,效果很好: {{item.name}

但是当我尝试使用不同的条件,例如

item.name:xxx=>颜色:红色

item.name:yyy=>颜色:黄色


item.name:aaa=>color:blue

您可以有条件地应用类:

<td ng-class="{red: item.name === 'xxx', yellow: item.name === 'yyy'}">
    {{ item.name }}
</td>
CSS:

在html中

<td ng-class="{item-color1: item.name == xxx , item-color2: item.name == yyy, item-color3: item.name == aaa}">

我不能给你一个确切的答案,因为你没有发布足够的代码,但看看这是否有帮助:

<div ng-if="item.name == xxx"><div ng-style="{'background-color': yourColor}"></div>

“yourColor”可以是像“red”这样的直接颜色名称,也可以是范围变量

如果您的颜色属于以下类别,也可以使用ng类别:

<div ng-class="{'myColor1': item.name == xxx, 'myColor2': item.name == yyy, 'myColor3': item.name == zzz}"></div>

希望这有帮助

试试这个

var-app=angular.module(“app”,[]);
应用程序控制器(“ctrl”,函数($scope){
$scope.items=[{“name”:“ali”},{“name”:“reza”},{“name”:“amir”}];
$scope.getStyle=函数(名称){
如果(名称==“阿里”)
返回{'color':'red'};
如果(名称=“reza”)
返回{'color':'blue'};
如果(名称==“阿米尔”)
返回{'color':'green'};
}
});

{{{item.name}

##控制器JS## 玉
感谢您的重播,但我不想使用css文件,我想直接在ng class指令中参数化颜色。感谢您的重播,但我不想使用css文件,我想直接在ng class指令中参数化颜色。您好。请添加一些细节和解释以及张贴的代码,以便帮助他人理解
<div ng-if="item.name == xxx"><div ng-style="{'background-color': yourColor}"></div>
<div ng-class="{'myColor1': item.name == xxx, 'myColor2': item.name == yyy, 'myColor3': item.name == zzz}"></div>
 var app = angular.module("app",[]);

    app.controller("ctrl" , function($scope){
  $scope.Unresolved = [{"status":"Assigned"},{"status":"Unassigned"},{"status":"CustomerWait"}];
  $scope.getStyle = function(status){
    console.log('color============>>>>>>>>>',status);
      if(status == "Unassigned")
        return {'background-color':'#d9534f'};
      if(status == "Assigned")
        return {'background-color':'#337ab7'};
      if(status == "CustomerWait")
        return {'background-color':'green'};
   });

       })
script(src='js/angularjs/angular.js')(you using version)
tr(ng-repeat='unresolved in unresolveds track by $index')
  td
    select.colorSel(ng-model='unresolved.status',ng-style='getStyle(unresolved.status)')
      option(value='Assigned',) Assigned
      option(value='Unassigned') Unassigned
      option(value='Customer wait') Customer wait