【发布时间】:2017-05-29 17:15:39
【问题描述】:
我有以下自定义指令:
angular.module('Interfaces').directive('Interfaces', function() {
return {
restrict : 'A',
scope : {
minor : '@'
},
templateUrl : 'interfaces/interfaces.template.html',
controller : [ '$scope', 'InterfacesService', function InterfaceController($scope, InterfacesService) {
$scope.interfacesService = InterfacesService;
$scope.label = 'Interfaces';
$scope.optional = ($scope.minor == "true");
if ($scope.optional) {
$scope.label = '';
}
$scope.getInterfaces = function getInterfaces() {
return $scope.interfacesService.getInterfaces($scope.minor);
};
} ]
};
});
还有下面的模板
<tr ng-class="{'optional': optional}"><td colspan="5">Just testing</td></tr>
<tr ng-class="{'optional': optional}" ng-repeat="interface in interfaces = (getInterfaces())" >
<td rowspan='{{interfaces.length}}' class='label-column' ng-if="$index === 0">{{label}}</td>
<td colspan='2' class='data'>{{interface.key}}</td>
<td colspan='2' class='data'>{{interface.value}}</td>
</tr>
我将此指令用作表格的一部分:
<table>
<tbody>
<!-- other table content -->
</tbody>
<tbody interfaces minor="true"></tbody>
<tbody interfaces minor="false"></tbody>
<tbody>
<!-- other table content -->
</tbody>
</table>
第一行只是为了测试目的而添加的。根据变量“可选”的值,它正确地具有“可选”类。 但是,由 ng-repeat 创建的那些表行永远不会设置“可选”类,无论“可选”变量的值是什么。
我找到了以下文章 Angular js Custom Directive on element with ng-repeat - Can't set CSS classes 这建议在我的指令中使用 -1001 的优先级,但该帖子中原始代码的 1001 和答案中建议的 -1001 对我的情况都没有影响。
为什么 ng-class 没有应用于带有 ng-repeat 的元素?
【问题讨论】:
-
你确定你找对地方了吗?第一行设置 td 上的类。下一个在 tr 上设置类。
-
@JB Nizet:很好,我错过了。我修复了我的帖子以确保两个
ng-class语句都在<tr>元素上。我在我的模板中进行了相同的更改,但遗憾的是问题仍然存在:ng-class在其自己的作品中,与ng-repeat结合没有。
标签: angularjs angularjs-directive angularjs-ng-repeat angularjs-ng-class