【问题标题】:ngResource - doesn't show data in browserngResource - 不在浏览器中显示数据
【发布时间】:2016-08-22 23:49:49
【问题描述】:

我对 ngResource 有疑问。 这是我的.factory

app.factory('sveKlupeServiceFactory', ['$resource',
 function($resource){
return $resource('myURL/to/json', {},{
// { method: 'getKlupe', q: '*' },
 query: { method: 'GET', params:{klupaId:'klupe'}, isArray:true}
 });

这是我的控制器

app.controller('klupeController', ['$scope', 'sveKlupeServiceFactory', function ($scope,sveKlupeServiceFactory){
$scope.klupe = sveKlupeServiceFactory.query();
  }]);

html 我有这个

<tr ng-repeat="klupa in klupe">
           <td>{{klupa.serial_number}}</td>
             <td>{{klupa.location_id}}</td>
             <td>{{klupa.type}}</td>
             <td>{{klupa.last_report_dt}}</td></tr>

问题: 在我的浏览器中,我有表格,但有空行。没有任何错误。

在我的应用中

var app = angular.module('App', [
'ngRoute',
'ngResource']);

有人可以帮我提些建议吗?

谢谢。

【问题讨论】:

  • 控制台有错误吗?
  • 您好,没有任何错误
  • 在你的控制器(或console.log)中放置一个断点并验证$scope.klupe是否包含数据
  • 在控制台日志中他们给了我 d {$promise: d, $resolved: false}$promise: d$resolved: truesmart_benches: Array[7]__proto__: Object controllers.js:26 d {$承诺:d,$resolved:假}

标签: angularjs factory ngresource


【解决方案1】:

如果你想要整个表数据,那么在工厂中不需要将id作为参数传递。在调用工厂方法时在控制器中进行以下更改。 使用 console.log() 检查响应

sveKlupeServiceFactory.query(function(res){
console.log(res);
$scope.klupe = res;
});

【讨论】:

  • thnx,我想要所有数据,然后当我单击特定行以显示该 ID 的 mi 数据。
【解决方案2】:

您应该使用承诺来获得响应。

$scope.klupe = sveKlupeServiceFactory.query();
$scope.klupe.$promise.then( function(result) { $scope.klupe = result });

【讨论】:

  • thnx,现在我有了这个,但还是一样,表是空的 $scope.klupe = sveKlupeServiceFactory.query(function(res){ $scope.klupe.$promise.then( function(result) { $scope.klupe = 结果;}); console.log($scope.klupe); });
  • 没有人,它是两行,就像我写的那样。再次检查我的代码。为什么查询方法有那个参数,它不应该有你示例代码显示的参数。
  • sry,我粘贴错了,我像你写的那样输入了我的 ctrl。并且有相同的。我需要哪些参数?我在论坛的主题中找到了这个参数。这是我的 json 如果这可以帮助 {"pametna_klupa":[{"id":1,"created_at":"2016-04-21T09:43:30+0200","modified_at":"2016-04-21T09: 43:30+0200","serial_number":"121sdfgde43547zuxh","token":"a12nii1h21h124slgk34jk3l242422skgfjk000wet55","location_id":4343,"type":"urban","description":"lipa klupa","is_active": true,"time_zone":null,"last_report_dt":null,"last_ip":null}]}
  • 现在唯一的改变应该是$scope.klupe = result.pametna_klupa[0] 以获取数组中响应的第一个元素。
  • thnx,但是我有很多数组,我想显示所有数组,然后,当我单击一个以显示该 klupa 的所有信息并制作 CRUD 时。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-26
  • 2021-03-04
  • 1970-01-01
  • 2022-01-20
  • 2021-11-25
  • 2017-03-24
相关资源
最近更新 更多