2016-04-22 2 views

Antwort

4

Für Arrays würden Sie indexOf, nicht in verwenden, die für Objekte

if (list.indexOf(this.id) !== -1) { ... } 

so

ng-class="{'class-1' : list.indexOf(this.id) !== -1, 'class-2' : list.indexOf(this.id) === -1}" 
+0

Jetzt ist es richtig :), +1 – alexmac

+0

Danke, es hat funktioniert! :) –

+0

'ng-class =" liste.indexOf (this.id)! == -1? 'Class-1': 'class-2' "' –

0

Blick auf den folgenden Code:

<html> 
<head> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script> 
    <style>.blue{background:blue;}</style> 
</head> 
<body ng-app="myApp" ng-controller="myCtrl"> 

    <p ng-class="{blue:present}">This is a paragraph. </p> 

    <script> 
     //Module declaration 
     var app = angular.module('myApp',[]); 
     //controller declaration 
     app.controller('myCtrl', function($scope){ 
      $scope.present = false; 
      $scope.colors = ['red','green','blue']; 
      angular.forEach($scope.colors, function(value, key){ 
       if(value == "green"){ 
        $scope.present = true; 
       } 
      }); 
     }); 
    </script> 
</body> 
</html> 

Hoffnung, es hilft, Ihr Problem!

Verwandte Themen