【发布时间】:2017-08-18 12:53:18
【问题描述】:
我想将我的 Rest-API 中的 JSON 数据保存在 $scope 变量中以供进一步使用。 问题是当代码执行时,我在 Firebug 中看到我已经成功获取了 JSON 数据,但问题是,我无法将它保存在我的 Scope 变量中,我不知道为什么。
我的 app.js
var app = angular.module('shop', ['ngRoute','ngResource'])
.factory('Customerservice', function ($resource) {
return $resource('http://localhost:8080/Shop/:customer',{customer: "@customer"});
})
.config(function ($routeProvider, $locationProvider) {
$routeProvider.when('/name/:ID', {
templateUrl : "Customer/customers.html",
controller : 'customerController'
});
})
.controller('customerController', function ($scope,Customerservice) {
$scope.customerinfo = Customerservice.get({customer: "Mark"});
alert($scope.customerinfo);
});
就像我说的,我有 JSON 数据,但问题出在我的控制器“customerController”中。我只是将警报功能放在我的代码中,以查看我的 $scope.customerinfo 中的内容。好吧,customerinfo 的内容只是对象:对象。 在使用 Firebug 进行调试时,我发现了一些奇怪的东西。看起来警报是在获取请求之前执行的。这可以解释为什么我的 $scope 变量中没有数据。任何人都可以在这里帮助我。
【问题讨论】:
-
如果您尝试警告对象,警告将仅显示 [object: objcet]。要查看警报中的值,您可以使用 JSON.stringify 函数对 json 对象进行字符串化。或者查看它,你可以使用console.log()。
-
使用控制台日志代替警报
-
做一件事,将调试器置于警报状态并打开 Chrome 开发者工具,当执行停止在该行时,只需将鼠标悬停在 $scope.customerinfo 上。您将看到 object 的内容。
标签: angularjs angular-resource ngresource