【问题标题】:unable to retrieve value inside the ng-repeat无法在 ng-repeat 中检索值
【发布时间】:2017-01-14 06:52:06
【问题描述】:

我正在尝试手动将值添加到从 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>

我怎样才能得到那里显示的价值测试。我希望在另一个 switch 案例或 ng-if 案例中使用该值

【问题讨论】:

    标签: html angularjs angularjs-scope


    【解决方案1】:

    我认为问题在于$scope.searchresponse 是一个数组,而您试图添加键值对,就像它是一个对象一样。将结构更改为

    $scope.searchresponse = {items: [<your-items-goes-here>], action: "test"} 
    

    因此您可以使用ng-repeat="item in searchresponse.items" 访问它,并且可以使用{{searchresponse.action}} 显示您的操作

    【讨论】:

    • 我已经在我的控制器中声明了$scope.searchresponse,我忘了在这里添加(现在已经更新了问题)。我遇到的唯一问题是访问稍后添加到 searchresponse 的操作变量
    【解决方案2】:

    您在 ng-repeat 中的绑定需要查看 details,因为这将是您数据的本地范围。所以绑定一些数据看起来像:

    <td> {{details.action}}</td>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多