我的表格如下
<tr ng-repeat-start="x in referral">
<td>{{x.id}}</td>
<td>{{x.name}}</td>
<td>{{x.region}}</td>
<td><button data-ng-click="request(x.id)">Request Info</button></td>
<td><span>{{x.upload_msg}}</span></td>
</tr>
代码如下:
referral.list($scope.user_id).then(function(response){
$scope.referral=response.data.data;
for (i=0;i< ($scope.referral).length; i++){
$scope.referral[i].upload_msg="";
}
});
最后一列最初是空的。 现在当有人点击按钮时,它会检查是否满足某个条件并使用 JS 中的以下函数显示消息
$scope.request=function(id){
data={"user_id":id};
referral.request(data).then(function(response){
console.log(response);
$scope.upload_msg=response.data;
});
}
功能很好,除了当我检查表中一行的状态时,它会在所有行中显示上传消息的值。我理解它是因为我在表中插入了一个范围变量。
恐怕我不知道如何单独显示每行的 upload_msg 值。
我们将不胜感激。
最佳答案
想法是将整个 x
对象传递给您的 request
方法,并相应地更新它。
$scope.request=function(x){
data={"user_id":x.id};
referral.request(data).then(function(response){
console.log(response);
x.upload_msg=response.data;
});
}
您的 $scope.upload_msg
属性是为整个 Controller 定义的,而不仅仅是表中的一行。因此,当您更新了 $scope.upload_msg
属性时,它会针对您表中的每一行进行更新。使用 x.upload_msg
,每行都有一个属性。
关于javascript - 在 ng-repeat 表中插入一个独立的范围变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42900464/