简体   繁体   English

在AngularJS中使用ng-repeat的动态类

[英]Dynamic classes using ng-repeat in AngularJS

This is not a duplicate. 这不是重复的。

In the other post, they are just doing a ternary operation. 在另一篇文章中,他们只是在做三元手术。 I wanna changes classes within ng-repeat. 我想改变ng-repeat中的类。

I have this piece of code with little bugs. 我有这段代码有一些小错误。

HTML : HTML

<div id="server-id-list-container" class="panel-body col-md-12 scrollbar">
    <div class="server-id-list-element" ng-class="serverIdLength > 12 ? 'col-md-3' : 'col-md-2'" ng-repeat="server in selection.serverIds">
        <p class="alert alert-info">{{server.serverId}}<span ng-click="removeServerId($index)" class="glyphicon glyphicon-remove"></span></p>
    </div>
</div>

Controller : 控制器

_.forEach($scope.selection.serverIds, function(a) {
    $scope.serverIdLength = a.serverId.length;
});

Scope Object: 范围对象:

[
    {
        "serverId": "loma1pwipdb2002",
        "serverName": "",
    },
    {
        "serverId": "shdmqprtp1",
        "serverName": "",
    }
]

When I enter "loma1pwipdb2002", the class becomes col-md-3 and since I am using ng-repeat applies for all elements. 当我输入“loma1pwipdb2002”时,该类变为col-md-3 ,因为我使用ng-repeat适用于所有元素。 I want the class to be applied only to serverIdLength > 12 and if its lesser than 12, col-md-2 should get applied. 我希望该类仅应用于serverIdLength> 12,如果小于12,则应该应用col-md-2

Please advice. 请指教。

Is it correct that you want to switch your class for each element of selection.serverIds list separately based on serverId string length? 是否要根据serverId字符串长度分别为selection.serverIds列表中的每个元素切换类是否正确? Need to know your selection.serverIds , is it your "Scope Object"? 需要知道你的selection.serverIds ,它是你的“范围对象”吗? If yes, then I would do just 如果是,那我就做

<div 
  class="server-id-list-element"
  ng-repeat="server in selection.serverIds"
  ng-class="server.serverId.length > 12 ? 'col-md-3' : 'col-md-2'"> ... </div>

The problem is that your $scope.serverIdLength is being calculated once for all the list. 问题是你的$scope.serverIdLength正在为所有列表计算一次。 While you want to have a dynamic class based on each item specific property. 虽然您希望根据每个项目特定属性拥有动态类。

Let's continue discussion if I didn't understand the issue and the entry conditions. 如果我不理解问题和进入条件,让我们继续讨论。

the issue seems to lie here: 问题似乎在于:

_.forEach($scope.selection.serverIds, function(a) {
    $scope.serverIdLength = a.serverId.length;
});

No matter what $scope.serverIdLength will always be set to the length of the last serverId. 无论$scope.serverIdLength将始终设置为最后一个serverId的长度。 That because it's a global variable and there is only one instance of it. 那是因为它是一个全局变量,只有一个实例。 This is why all your classes match. 这就是你所有课程都匹配的原因。 They all reference the same variable. 它们都引用相同的变量。

Instead like @dhilt suggested ditch the controller code and acccess the length in the dom: 相反像@dhilt建议抛弃控制器代码并在dom中控制长度:

ng-class="server.serverId.length > 12 ? 'col-md-3' : 'col-md-2'"

试试看:

ng-class="{'col-md-3':server.serverId.length > 12, 'col-md-2':server.serverId.length <= 12}"

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

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