【发布时间】:2014-12-11 23:06:09
【问题描述】:
我有一个列出客户的表格,对于也有客户列表的客户,允许用户单击表格行以显示该列表。 HTML 如下所示:
<tbody ng-repeat ="item in customers | clientSettingsFilter:customerId">
<tr ng-class="{'row-hover': item.clientSettings.length>0}" ng-click="item.showClients=!item.showClients">
<td><span ng-class="{true:'glyphicon glyphicon-chevron-down', false:'glyphicon glyphicon-chevron-right'}[item.showClients]" ng-hide="item.clientSettings.length==0"></span></td>
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td><button type="button" class="btn btn-default btn-xs" ng-click="go('/client-config/defaults/'+item.id)">Defaults</button></td>
</tr>
<tr ng-show="item.showClients">
..... // client data
我的奇怪行为是这样的:
如果我在客户数据集中未定义“showClients”属性,一切都会按预期工作,除了 V 形图标一开始没有显示。单击一次后,它会显示并且切换按预期工作。我在想这可能是因为 ng-class 正在寻找 true 或 false,而 undefined 不满足其中任何一个。
如果我将 showClients 属性预定义为 true 或 false,则 V 形在页面加载时正确显示并且客户端列表正确显示,但切换不再起作用,就好像 ng-click 没有做任何事情或某些原因无法更改该值。我不确定如何调试这样的内联指令。
编辑
根据请求,这是来自控制器的相关代码:
filter('clientSettingsFilter', function () {
return function (customers, customerId) {
var filtered = [];
if (!customerId)
return filtered;
customerId = customerId.toLowerCase();
angular.forEach(customers, function (item) {
if (item.id.toLowerCase().indexOf(customerId) !== -1) {
// format some text properties, omitted for brevity
// if this line is uncommented, the ng-click does not work
//item.showClients = false;
filtered.push(item);
}
});
return filtered;
};
});
【问题讨论】:
-
请加你的js代码
-
@Floradu88 已编辑。控制器中唯一发生的事情是我是否设置了 showClients 属性。
-
能否请您给我们一个详细的客户对象以更好地了解流程?
标签: angularjs angularjs-directive