我正在尝试手动将值添加到我从REST API获得的响应中,并在View中使用它。问题是我无法访问ng-repeat中手动添加的项目。让我用我的例子来解释。这是我从REST API获得的搜索响应。我在搜索响应中添加了这样的值。
$scope.searchresponse = [{
"items": [{
"employeeId": "ABC",
"type": "D",
"alive": "Yes"
}, {
"employeeId": "DEF",
"type": "D",
"alive": "Yes"
}, {
"employeeId": "NPK",
"type": "D",
"alive": "Yes"
}, {
"employeeId": "PKN",
"type": "A",
"alive": "Yes"
}],
"more": false
}];
$scope.searchresponse.action = "Test"
console.log($scope.searchresponse);// I am able to see the test value here
这是我的HTML
<tr ng-repeat="details in searchresponse">
<td class=list align=center ng-switch="details.type">
<span
ng-switch-when="D">SINGLE</span><span
ng-switch-when="E">MULTIPLE</span>
<td> {{$scope.searchresponse.action}}</td> // - Tried this didnt work
<td> {{searchresponse.action}}</td> // - tried this didnt work .
</td>
如何在那里显示价值测试。我希望在另一个交换机案例或ng-if案例中使用该值
答案 0 :(得分:2)
我认为问题是$scope.searchresponse
是一个数组,并且您尝试添加键值对,就像它是一个对象一样。如何将结构更改为
$scope.searchresponse = {items: [<your-items-goes-here>], action: "test"}
因此您可以使用ng-repeat="item in searchresponse.items"
访问它,并且可以使用{{searchresponse.action}}
答案 1 :(得分:0)
ng-repeat中的绑定需要查看details
,因为这将是您数据的本地范围。所以绑定一些数据看起来像:
<td> {{details.action}}</td>