1

In my Html I displayed two times "OUI" I think because I have two default installations (delestage, default command') how can I filter them one by one using ng-class and ng-repeat.

 <div  ng-repeat="value in installationsHelper" class="col-lg-6 col-md-6 col-sm-6 col-xs-6 text-left">
                    <div ng-repeat="value1 in value.InstallationDefautsTypes.value | filter :  'D'">{{value.InstallationDefautsTypes.value}}
                        <span ng-class="value1 ? 'mDefault' : 'mDanger'">
                            OUI
                        </span>

    <script>
     angular.module('myApp', []).controller('myController', function($scope) {      
 $scope.installationsStates = [
                {
                    Installations: {
                        id: 1
                    },
                    InstallationsDefautsTypes : {
                       Delestage:
                     {
                        name: "Delestage", 
                        value : false, 
                        id: 1}
                     defaut command:
                        {
                        name: "Delestage", 
                        value : false, 
                        id: 1
                     }
                      }
                    },
                    defaultsInstallations: {
                        0:"defaut",
                        1:"Delestage"
                    }
                },
                {
                    Installations: {
                        id: 2
                    },
                    InstallationsDefautsTypes : {
                     Delestage:{
                        name: "Delestage", 
                        value : false, 
                        id: 1
                      }

        });
4
  • Do you need filter data or color text by a boolean? Commented Jul 24, 2018 at 12:01
  • Heloo Ans have you an idea please Commented Jul 24, 2018 at 12:01
  • i need to filter color text by boolean and i have to defaults Commented Jul 24, 2018 at 12:02
  • Make your question more clear that what you want to show or what do you mean by filter? Commented Jul 24, 2018 at 12:04

2 Answers 2

4

By documentation

The ngClass directive allows you to dynamically set CSS classes on an HTML element by databinding an expression

If you want change color of the text by a boolean you can use ng-class in this way:

<any class="ng-class: expression;"> ... </any>

in your case:

<span ng-class = "ng-class="value.InstallationsDefautsTypes.value ? 'red' : 'black'">
                OUI
            </span>

where red or black have to be defined css class.

Indeed for filtering data you have two different method:

  1. Inside the html
  2. In a function inside controllers

By documentation

{{ expression [| filter_name[:parameter_value] ... ] }}

In this case thanks to the "pipe" you can add a filter in your code.

E.g. HTML:

<ul>
  <li ng-repeat="friend in person.friends | startsWithA">
    {{ friend }}
  </li>
</ul>

JS:

app.filter('startsWithA', function () {
  return function (items) {
    var filtered = [];
    for (var i = 0; i < items.length; i++) {
      var item = items[i];
      if (/a/i.test(item.name.substring(0, 1))) {
        filtered.push(item);
      }
    }
    return filtered;
  };
});

You can also filter your data on controller (you must add to the scope a new filtered list) and then add inside ng-repeat your filtered list.

Sign up to request clarification or add additional context in comments.

3 Comments

How you can do that inside html please
the filter or the ng-class?
@hakim don't edit my answer with your code, at most add a comment.
0

If you want to change color dynamically, you can use ngStye.

 <span [ngStyle]="{'color': value.InstallationsDefautsTypes.value ? 'red' : 'black'}"> 
       OUI
  </span>

i used this :

<div  ng-repeat="value in installationsHelper" class="col-lg-6 col-md-6 col-sm-6 col-xs-6 text-left">

                        <span ng-class="value.InstallationsDefautsTypes.value ? 'mDefault' : 'mDanger'">
                            OUI
                        </span>

                </div>

11 Comments

i changed the Json you can see now please
i want to filter InstallationsDefautsTypes , how i can filter a installationsDefautsTypes please
be careful, this code it could be works on angular2+ not in angularjs.
yes but how i can filter the installationsDefautsTypes can you help me please
are you able to create a function on a controller?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.