1

I am working on angularJS where my codes are fine but i am stuck at one place where i cant find why extra quotes are required in ng-class-even directive.

In this line <tr ng-repeat="x in records" ng-class-even='"striped"'> why striped is written like this "'striped'" instead of this "striped"

.striped {
    color:white;
    background-color:black;
}
<body ng-app="myApp">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<table ng-controller="myCtrl">
<tr ng-repeat="x in records" ng-class-even="'striped'">
  <td>{{x.Name}}</td>
  <td>{{x.Country}}</td>  
</tr>
</table>

<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.records = [
    {
      "Name" : "Alfreds Futterkiste",
      "Country" : "Germany"
    },
    {
      "Name" : "Berglunds snabbköp",
      "Country" : "Sweden"
    },
    {
      "Name" : "Centro comercial Moctezuma",
      "Country" : "Mexico"
    },
    {
      "Name" : "Ernst Handel",
      "Country" : "Austria"
    }
  ]
});
</script>

</body>
Gaurav Aggarwal
  • 9,809
  • 6
  • 36
  • 74

3 Answers3

3

ng-class-even expects an expression but you are willing to assign a hard coded string and strings are wrapped in quotes to be valid token or else angular parser will look for model-name having name as striped

Rayon
  • 36,219
  • 4
  • 49
  • 76
1

This is because ng-class-even allows to give a expression. In your question you says:

n this line why striped is written like this "'striped'" instead of this "striped"

if you will try be do this, Angular thinks that striped is a variable attached in the $scope. So using 'striped' tells to angular that the expression that you passed to the directive is a string value

  • but why only for `ng-class-even` or `ng-class-odd` and not in `ng-` class` – Gaurav Aggarwal Jun 09 '16 at 06:39
  • 1
    @GauravAggarwal And for ngClassOdd the same. ngClass has different purpose and notation. Did you even read documentation, would (probably) solve many of your questions. – dfsq Jun 09 '16 at 06:41
  • @GauravAggarwal I suggest to check official documentation first, it's often enough. – dfsq Jun 09 '16 at 06:46
1

beacuse ng-class-even and odd both requires expression and you need String so you have to write String because you need hard coded string here is documentation for ng-class-even

https://docs.angularjs.org/api/ng/directive/ngClassEven

you can see very good example at there

jitendra varshney
  • 3,484
  • 1
  • 21
  • 31