Javascript 在行中重复相同的数据
为什么我在表的所有行中得到相同的值。表中有行,当单击行时,会打开另一行,其中包含附加信息,并且每一行包含与上次单击的行相同的数据。可能是因为我在同一个对象中保存数据,但我真的不知道如何解决它。我从带有factor的Javascript 在行中重复相同的数据,javascript,html,angularjs,Javascript,Html,Angularjs,为什么我在表的所有行中得到相同的值。表中有行,当单击行时,会打开另一行,其中包含附加信息,并且每一行包含与上次单击的行相同的数据。可能是因为我在同一个对象中保存数据,但我真的不知道如何解决它。我从带有factor的json文件中获取数据,所以您不会感到困惑,我的数据来自这里的代码。多谢各位 “严格使用”; 角度.module('sbAdminApp') .controller('TreeTableCtrl',TreeTableCtrl); 函数TreeTableCtrl($scope、$htt
json
文件中获取数据,所以您不会感到困惑,我的数据来自这里的代码。多谢各位
“严格使用”;
角度.module('sbAdminApp')
.controller('TreeTableCtrl',TreeTableCtrl);
函数TreeTableCtrl($scope、$http、factor){
函数示例(){
factor.getUse().then(函数(响应){
$scope.users=response.data;
log($scope.users);
});
}
示例();
$scope.togglePerson=函数(索引){
factor.getChi().then(函数(响应){
$scope.indx=索引;
$scope.child=response.data;
log($scope.child);
$scope.childPar=$scope.child[index];
log($scope.childPar);
});
};
}
身份证件
名称
姓
{{user.id}
{{user.name}
{{user.lastName}
地址
{{childPar.address}
数
{{childPar.mobNumb}
当然,它应该是相同的,您只有一个属性来处理额外的信息,即$scope.childPar
与其这样做,不如根据您的情况操作数据
试试这个
function example() {
factor.getUse().then(function (response) {
//Remap the model, [data] -> user, [otherInfo] -> other info
var users = response.data.map(function(value) {
return { data: value, otherInfo: null }
});
$scope.users = users;
console.log($scope.users);
});
}
example();
$scope.togglePerson = function (index) {
factor.getChi().then(function (response) {
$scope.indx = index;
$scope.child = response.data;
console.log($scope.child);
//Populate the otherInfo from the response
$scope.users[index].otherInfo = response.data;
console.log($scope.childPar);
});
};
HTML
<div class="panel panel-default" style="background: #fcf8e3">
<div class="panel-body">
<table st-safe-src="leads" class="table table-hover ">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat-start="user in users track by $index" ng-init="parentIndex=$index"
ng-click="togglePerson($index); viewChild=!viewChild ">
<td> {{user.data.id}}</td>
<td>{{user.data.name}}</td>
<td>{{user.data.lastName}}</td>
</tr>
<tr ng-show="viewChild" ng-hide="indx!=$index" >
<td style="width: 450px">
<table class="table ">
<tbody>
<tr>
<th style="width: 200px">
Address
</th>
<td style="width: 200px">{{user.otherInfo.address}}</td>
<th style="width: 200px">
Number
</th>
<td style="width: 200px">{{user.otherInfo.mobNumb}}</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr ng-repeat-end></tr>
</tbody>
</table>
</div>
</div>
身份证件
名称
姓
{{user.data.id}
{{user.data.name}
{{user.data.lastName}
地址
{{user.otherInfo.address}
数
{{user.otherInfo.mobNumb}
作为$scope.togglePerson
在控制器中声明,您在everychild中引用相同的$scope道具。