为什么 ng-class-even 需要额外的引号

Why ng-class-even required extra quotes

我正在 angularJS 工作,我的代码很好,但我被困在一个地方,我无法找到为什么 ng-class-even 指令中需要额外的引号。

这一行<tr ng-repeat="x in records" ng-class-even='"striped"'>为什么striped是这样写的"'striped'"而不是这个"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>

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

这是因为ng-class-even允许给出expression。 在你的问题中你说:

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

如果您尝试这样做,Angular 认为 striped 是附加在 $scope 中的变量。所以使用 'striped' 告诉 angular 你传递给指令的表达式是一个 字符串值

因为 ng-class-even 和 odd 都需要表达式 你需要 String 所以你必须写 String 因为你需要硬编码的字符串 这是 ng-class-even

的文档

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

你可以在那里看到很好的例子