【问题标题】:Fetch .json with AngularJS $resource使用 AngularJS $resource 获取 .json
【发布时间】:2018-04-19 02:23:37
【问题描述】:

所以我有一个网络服务器来处理我的 .json 文件。

在客户端有一个简单的服务:

var service = angular.module('services.service', ['ngResource']);

service.factory('Service', ['$resource',

    function($resource){
        return $resource('json/:serviceId.json', {},
            {

            }
        );

    }
]);

但是当我从控制器调用它时:

$scope.service = Service.get({serviceId : id} );

我没有将任何内容写入 $scope.service

我检查了我的网络,.json 文件到达,但没有处理?

不知道怎么办

【问题讨论】:

  • JSON 格式是否错误?你能把它贴在这里吗?你也可以尝试捕捉错误Service.get({serviceId: id}).$promise.then(console.log,console.log);
  • Thnx 伙计,你救了我,它的格式不正确。在这件事上浪费了一个小时。发表答案,我会接受的。

标签: angularjs angular-services ngresource


【解决方案1】:

JSON 可能格式不正确。您可以通过记录来更详细地查看调用的响应:

Service.get({serviceId: id}).$promise['finally'](console.log);

【讨论】:

    【解决方案2】:

    在控制器中使用它。

     Service.get({serviceId : id} ).$promise.then(function(d){
        $scope.service = d;
        })
        .catch(function(err){
        console.log(err);
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-12
      • 2020-04-09
      • 2013-08-16
      相关资源
      最近更新 更多