Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change color with ng-style and different conditions

Tags:

angularjs

I want to parameter changes of color of my td so I try this and it's working fine: {{item.name}}

but when I try to use different conditions for example for

item.name : xxx =>color : red

item.name: yyy =>color : yellow

item.name: aaa =>color : blue

like image 249
n.Ken Avatar asked Oct 30 '25 05:10

n.Ken


1 Answers

try this.

var app = angular.module("app",[]);

app.controller("ctrl" , function($scope){
  $scope.items = [{"name":"ali"},{"name":"reza"},{"name":"amir"}];
  
  
  $scope.getStyle = function(name){
    if(name == "ali")
      return {'color':'red'};
    if(name == "reza")
      return {'color':'blue'};
    if(name == "amir")
      return {'color':'green'};
    }
  
  
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl" class="panel-group" id="accordion">
  
  <div ng-repeat="item in items">
      <p ng-style ="getStyle(item.name)">{{item.name}}</p>
    </div>
            
</div>
like image 53
Hadi J Avatar answered Nov 01 '25 03:11

Hadi J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!