简体   繁体   English

ng-repeat ng-class条件的语法

[英]Syntax of ng-repeat ng-class conditional

Hi I'm trying to resolve the following html so that it applies the css class stored in $root.buttonClass='btn-primary' (for example) 嗨,我正在尝试解析以下html,以便它应用存储在$root.buttonClass='btn-primary'的css类(例如)

<li ng-controller="controllerCtrl">
   <a ng-class="{'$root.buttonClass':viewingContext=='{{entity}}'}" 
      ng-repeat="entity in entities" 
      ng-click="setContext(entity)" 
     type="">
     <span class="label bg-info pull-right" 
           ng-class="{hidden:viewingContext!='{{entity}}', show:viewingContext=='{{entity}}'}">
           viewing</span>
           {{entity}}</a>
</li>

The controller is fairly simple: 控制器非常简单:

  $scope.setContext = function (entity) {
    console.log('set the context as: ' + entity);
    if (entity == $scope.entities[0]) {
      $scope.$root.buttonClass = 'btn-primary';
      $scope.viewingContext = entity;
    }
    if (entity == $scope.entities[1]) {
      $scope.$root.buttonClass = 'btn-warning';
      $scope.viewingContext = entity;
    }
    if (entity == $scope.entities[2]) {
      $scope.$root.buttonClass = 'btn-danger';
      $scope.viewingContext = entity;
    }
  };

The line in the tag might be the problem! 标签中的行可能是问题所在!

<a ng-class="{'$root.buttonClass':viewingContext=='{{entity}}'}" 
   ng-repeat="entity in entities" 
   ng-click="setContext(entity)" 
   type="">

Update: 更新:

Attempted to make a js fiddle http://jsfiddle.net/gn6b4ng8/2 尝试制作js小提琴http://jsfiddle.net/gn6b4ng8/2

You can't have a dynamic key name in an object. 您不能在对象中使用动态键名。 From the documentation , it looks like you're trying to use the second type of expression, an object, but violating the structure "If the expression evaluates to an object, then for each key-value pair of the object with a truthy value the corresponding key is used as a class name." 文档中看 ,您似乎正在尝试使用第二种类型的表达式,即对象,但违反了结构“如果表达式求值为对象,则对于具有真值的对象的每个键值对,相应的键用作类名。” The key would be used as a class name, as is. 该键将原样用作类名。

It looks like what you want to be using the first type of expression possible for ng-class "If the expression evaluates to a string, the string should be one or more space-delimited class names." 看起来您想使用第一种类型的ng-class表达式是什么:“如果表达式的计算结果为字符串,则该字符串应为一个或多个以空格分隔的类名。” If instead you do something like ng-class="$root.buttonClass" . 相反,如果您执行类似ng-class="$root.buttonClass" That should solve your problem by then resolving to your class name you store in buttonClass. 那应该解决您的问题,方法是解析为存储在buttonClass中的类名。 You can see an example in the documentation where it uses <p ng-class="style">Using String Syntax</p> 您可以在文档中看到一个示例,该示例使用<p ng-class="style">Using String Syntax</p>

I don't think ngClass will work the way you want it. 我认为ngClass不会按您想要的方式工作。

Technically you could achieve your goal with ngClass , only if you recompile the elements after each change. 从技术ngClass ,只有在每次更改后重新编译元素,才可以使用ngClass实现目标。 This way, Angular will apply the changes in the value of ngClass to your element. 这样,Angular会将ngClass值的ngClass应用于您的元素。

I don't think it's a nice way to do it. 我认为这不是一个好方法。

Luckily, this is Angular, and you can create your own functionality using directives. 幸运的是,这是Angular,您可以使用指令创建自己的功能。 You can create a directive, let's call it dynamicClass , that will have a similar behavior to that of ngClass , but will respond to changes in the class name: 您可以创建一个指令,将其称为dynamicClass ,它的行为与ngClass相似,但是会响应类名的更改:

myApp.directive('dynamicClass', function($compile) {
    return {
        restrict: 'A',
        link: function($scope, element, attrs) {
            $scope.$watch(function() {
                return attrs.dynamicClass;
            }, function(newVal, oldVal) {
                var condition = newVal.match(/:(.*?)\}$/)[1];
                var cls = newVal.match(/^\{'?(.*?)'?:/)[1];
                var oldCls = oldVal.match(/^\{'?(.*?)'?:/)[1];
                element.removeClass(oldCls);
                if ($scope.$eval(condition)) {
                    element.addClass(cls);
                }    

            });
        }
    };
});

usage: 用法:

<a dynamic-class="{'{{$root.buttonClass}}':viewingContext=='{{entity}}'}" 
  ng-repeat="entity in entities" 
  ng-click="setContext(entity)">

I forked your fiddle to test it. 我分叉了你的小提琴以进行测试。 It works . 它有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM