Gaurav Aggarwal Gaurav Aggarwal - 6 months ago 17
AngularJS Question

Why ng-class-even required extra quotes

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>




Answer

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