【发布时间】:2017-08-29 13:20:26
【问题描述】:
我的 get 方法正在响应我想要在表中呈现的 JSON 数据。 这是我 console.log 在我的 get 方法中声明的变量时的输出:
这是我的控制器方法:
$scope.getProdukt = function (searchText, typeVariable) {
var results = produktService.searchPakninger(searchText, typeVariable);
angular.forEach(results,
function (value, key) {
console.log(key);
console.log(value);
console.log(value.status.status + ' her er status.status');
$scope.searchValue = value;
});
};
我似乎无法正确引用对象来获取我想要的信息。 Bellow 是我想在其中呈现数据的表格。{{searchValue}} 在一个表格单元格中返回整个对象。我还需要一个ng-repeat,因为可以有多个对象。
<table class="table" id="mixTables">
<thead>
<tr>
<th>GTIN</th>
<th>EPDNR</th>
<th>NAVN</th>
<th>Handling</th>
</tr>
</thead>
<tbody ng-if="!searchEmpty">
<tr ng-repeat="obj in searchValue">
<td>GTIN: nummer: {{obj.GTIN}}</td>
<td>Kategori navn: {{obj.KategoriNavn}}</td>
<td>Kategori kode: {{obj.KategoriKode}}</td>
<td><button class="btn btn-green pull-right" type="button" ng-click="addProdukt()">Velg <span class="fa fa-plus-circle"></span></button></td>
</tr>
</tbody>
</table>
我已经更新了我的表格,但没有数据是我在表格中呈现的 obj 数据。我不认为 obj.GTIN 是引用 json 对象中数据的正确方法。带有承诺的解决方案在控制器中不起作用。
----------------------------------- - - - -更新 - - - - - - - - - - - - - - - - - - - - - ------------------------------------
我也在尝试在控制器中做出承诺。这是我到目前为止所拥有的:
$scope.getProdukt = function (searchText, typeVariable) {
var results2 = produktService.searchPakninger(searchText, typeVariable)
.then(function (response) {
var object = JSON.stringify(this.response);
//$scope.searchValue = response.data;
//$scope.isEmpty = false;
console.log('async promise = ' + object);
});
}
控制台日志打印出对象变量的未定义。我也一直在尝试 JSON.parse() ,但没有运气。对于有效的 searchText,response.length 为 1 或更多。
【问题讨论】:
-
使用 angular.forEach 有什么意义?你到底想做什么?
-
forEach 将遍历从 API 收到的所有结果。我正在尝试在表格的每个单元格中分配不同的值。
标签: javascript html angularjs json