change the font color based on value angular js

后端 未结 4 887
既然无缘
既然无缘 2020-12-29 09:32

I am using AngularJS and I use ng-repeat to loop and show the details in the page.

Is there a possibility to change the font color based on the value?

相关标签:
4条回答
  • 2020-12-29 09:52

    You can achieve this with following code:

    <li ng-repeat="s in vm.States" >                        
      <span>{{s.Name}}</span>
      <span ng-class="{'color-red': s.state === 'Error', 'color-blue': s.state === 'Warning', 'color-green': s.state === 'Ignored'}">{{s.state}}</span>
    </li>
    

    Where 'color-red', 'color-blue', 'color-green' are your css classes.

    See it in plunker.

    Check documentation about ng-class.

    0 讨论(0)
  • 2020-12-29 09:56

    You can do it very easily by ng-class and css.

    CSS code

    .Error{color: red;}
    .Warning{color: blue;}
    .Ignored{color: green;}
    

    Html code

    <li ng-repeat="s in vm.States" >                        
      <span>{{s.Name}}</span>
      <span ng-class="s.state">{{s.state}}</span>
    </li>
    
    0 讨论(0)
  • 2020-12-29 10:13

    It might be worth also taking a look over ngStyle which is the same as ngClass but provides conditional inline syling such as

    <li ng-repeat="s in vm.States" >                        
      <span>{{s.Name}}</span>
      <span ng-style="{'color: red': s.state === 'Error', 'color: blue': s.state === 'Warning', 'color: green': s.state === 'Ignored'}">{{s.state}}</span>
    </li>
    
    0 讨论(0)
  • 2020-12-29 10:17

    See this example

    var myApp = angular.module('myapp',[]);
    
    myApp
        .controller('MyCtrl1', function ($scope) {
            $scope.vm = {
                States: [
                    {
                        Name: 'Error',
                        state: 'error',
                        color: 'red'
                    },
                    {
                        Name: 'Warning',
                        state: 'warning',
                        color: 'blue'
                    },
                    {
                        Name: 'Ignored',
                        state: 'ignored',
                        color: 'green'
                    }
                ]
            };
        })
    .red{
        color: red;
    }
    
    .blue{
        color: blue;   
    }
    
    .green{
        color: green;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    <div ng-app="myapp">
        <div ng-controller="MyCtrl1">
            <li ng-repeat="s in vm.States" >                        
              <span ng-class="s.color">{{s.Name}}</span> - 
              <span ng-class="s.color">{{s.state}}</span>
             </li>
        </div>
     </div>

    0 讨论(0)
提交回复
热议问题