Angularjs 如何更新角度表中的列数
我想更改修改布尔变量时的列数。 检查我的示例():Angularjs 如何更新角度表中的列数,angularjs,html-table,Angularjs,Html Table,我想更改修改布尔变量时的列数。 检查我的示例(): angular.module('ui.bootstrap.demo',['ngAnimate','ngSanitize','ui.bootstrap']); angular.module('ui.bootstrap.demo')。controller('myCtrl',function($scope,$log){ $scope.debug={}; $scope.fields=[{ 标题:“第一”, hideField:!$scope.debu
angular.module('ui.bootstrap.demo',['ngAnimate','ngSanitize','ui.bootstrap']);
angular.module('ui.bootstrap.demo')。controller('myCtrl',function($scope,$log){
$scope.debug={};
$scope.fields=[{
标题:“第一”,
hideField:!$scope.debug.flag
},
{
标题:“第二”
},
{
标题:“第三”
},
{
头球:“第四”
},
];
$scope.entries=[{
第一个:“你好”,
第二个:“hello2”,
第三个:“你好”,
第四:“你好”
}, ]
$scope.myBool=true;
});代码>
刷新场!debug.flag={{!!debug.flag}}
表1的property.hideField=true
{{property.header}}
{{entry[property.header]}{{{!!debug.flag}}
表2中的ng if=>property.hideField=false
{{property.header}}
{{entry[property.header]}{{{!!debug.flag}}
表3无ng if
{{property.header}}
{{entry[property.header]}{{{!!debug.flag}}
如果修改其隐藏字段
属性的值,则标题将变为可见。但你没有那样做。您正在修改$scope.debug.flag
的值
hideField
是使用$scope.debug.flag
值初始化的,这一事实不会在每次更改$scope.debug.flag
时神奇地更改hideField
就像你做的那样
var a = 1;
var b = a;
a = 42;
b的值仍然是1。不是42。更改$scope.debug.flag
的值不会更改隐藏字段的值。因为,它在控制器加载时已经初始化。这里可以应用的解决方法是将hideField
绑定到方法,并在ng if
中对其进行评估。下面是示例
JS:
HTML:
表1的property.hideField=true
{{property.header}}
{{entry[property.header]}{{{!!debug.flag}}
但这不是一个干净的解决方案。但是,仍然可以解决您的问题。这发生在基本类型上,而不是对象上:let a={x:1},b=a;a、 x=42代码>和b.x将是肯定的,但这不是你正在做的。hideField并不像您的示例中的b
那样引用您更改其属性的对象。它指的是一个布尔值。但是,按照您在评论中的建议执行操作确实是修复代码的一种方式。然后,修改字段的变量fields.hideField
以指向对象而不是值,并修改ng if
中跟踪的属性,将优雅地解决此问题!!我想要干净的解决方案,我找到了。我会很快更新。谢谢你的贡献
$scope.fields = [{
header: "first",
hideField: function() {
return !$scope.debug.flag;
}
}, {
header: "second"
}, {
header: "third"
}, {
header: "fourth"
}, ];
<h2 class="label label-info">table 1 with property.hideField = true</h2>
<table class="table table-hover">
<tr>
<!-- Headers of list-->
<th ng-repeat="property in fields" ng-if="!property.hideField()">{{property.header}}
</th>
</tr>
<tbody>
<tr ng-repeat="entry in entries">
<td ng-repeat="property in fields" ng-if="!property.hideField()">
{{entry[property.header]}} {{!!debug.flag}}
</td>
</tr>
</tbody>
</table>