javascript Angular js ng 与条件 ng 类重复,不应用 css 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20491974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:37:13  来源:igfitidea点击:

Angular js ng repeat with conditional ng class not applying css class

javascriptangularjs

提问by user3086298

I have a ng repeat with a ng class that doesn't apply the css class in the case where my css class has a hyphen in the name:

如果我的 css 类的名称中有一个连字符,我有一个 ng 重复,该 ng 类不应用 css 类:

<li
  ng-repeat="item in items"
  ng-class="{'i-someclass' : item.Id > 10}">
  {{item .name}}
</li>

Am I doing anything wrong? If I change the css class name to isomeclass it works. AngularJS v1.0.7

我做错了什么吗?如果我将 css 类名更改为 isomeclass 它可以工作。AngularJS v1.0.7

回答by Davin Tryon

For me, your code works as is.

对我来说,您的代码按原样运行。

Here is a plunkerthat demonstrates the code.

这是一个演示代码的plunker

js:

js:

var app = angular.module('myApp', []);

app.controller('Ctrl', function($scope) {

  $scope.items = [

    { Id: 1, name: "item1" },
    { Id: 10, name: "item1" },
    { Id: 11, name: "item1" }
    ];

  });

html:

html:

<html ng-app="myApp">

  <head>
    <script data-require="angular.js@*" data-semver="1.2.4" src="http://code.angularjs.org/1.2.4/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="Ctrl">
    <h1>Hello Plunker!</h1>
    <ul>
      <li
        ng-repeat="item in items"
        ng-class="{'i-someclass' : item.Id > 10}">
        {{item .name}}
      </li>
    </ul>
  </body>

</html>

css:

css:

.i-someclass {
  color: red;
}