Restangular 数据,进入列表的 $scope
Restangular data, getting into $scope for a list
一整天都在与 API 东西搏斗,并决定使用 Restanglar。确实在将数据取出并放入 $scope 时遇到问题。
我知道它不仅仅是从 API 返回的 JSON,还有许多其他内部方法等。但是当我取出数据时,我可以通过 console.log 看到它埋在调试中的某个地方,但我似乎无法将它放入 $scope 以在我之前工作正常的视图中使用它。
我怎样才能将这些数据输出到我的 $scope 以及我的视图中?
型号
angular.module('horse', ['restangular'])
.config(function(RestangularProvider) {
RestangularProvider.setBaseUrl('http://url/api');
RestangularProvider.setResponseInterceptor(
function(data, operation, what) {
if (operation == 'getList') {
return data[what];
}
return data;
});
});
控制器
angular
.module('horse')
.controller("IndexController", function ($scope, Restangular) {
$scope.horse = null;
$scope.showSpinner = true;
Restangular.all('horse').getList().then(function(horse) {
$scope.horse = horse;
console.log($scope.horse);
});
});
API 回应
{"error":false,"horse":[{"id":"1","name":"horse 2"},{"id":"2","name":"horse 2"}]}
编辑 1
矩形响应
[Object, Object, route: "horse", getRestangularUrl: function, getRequestedUrl: function, addRestangularMethod: function, clone: function…]
编辑 2
我也试过这个 - https://github.com/mgonto/restangular#using-values-directly-in-templates
$scope.horse = Restangular.all('horse').getList().$object;
这只会导致输出一个空数组。我还尝试删除 setResponseInterceptor
并修改 api 的结构以直接生成没有元数据(错误等)的数据数组,没有乐趣:(
假设您显示为 "API response" 的内容是从控制器中的 console.log 输出的内容,看来您需要做的就是将示波器模型设置为 属性 "horse" 在这样的响应数据中:
$scope.horse = horse.horse;
由于读起来很奇怪,您应该将 .then 回调的参数名称更改为数据,这将是一个更加不可知的标准参数名称。如果你做了那个改变,你可以从你的回调中将你的马数据设置到你的范围模型,如下所示:
$scope.horse = data.horse;
如果我误解了你的问题,请告诉我。希望这对您有所帮助。
数据似乎正在通过。我注意到你在使用类固醇,你检查过标记而不仅仅是控制台吗?
确保将范围微调器设置为 false,以确保在数据通过时隐藏微调器。
$scope.ShowSpinner = false;
一整天都在与 API 东西搏斗,并决定使用 Restanglar。确实在将数据取出并放入 $scope 时遇到问题。
我知道它不仅仅是从 API 返回的 JSON,还有许多其他内部方法等。但是当我取出数据时,我可以通过 console.log 看到它埋在调试中的某个地方,但我似乎无法将它放入 $scope 以在我之前工作正常的视图中使用它。
我怎样才能将这些数据输出到我的 $scope 以及我的视图中?
型号
angular.module('horse', ['restangular'])
.config(function(RestangularProvider) {
RestangularProvider.setBaseUrl('http://url/api');
RestangularProvider.setResponseInterceptor(
function(data, operation, what) {
if (operation == 'getList') {
return data[what];
}
return data;
});
});
控制器
angular
.module('horse')
.controller("IndexController", function ($scope, Restangular) {
$scope.horse = null;
$scope.showSpinner = true;
Restangular.all('horse').getList().then(function(horse) {
$scope.horse = horse;
console.log($scope.horse);
});
});
API 回应
{"error":false,"horse":[{"id":"1","name":"horse 2"},{"id":"2","name":"horse 2"}]}
编辑 1
矩形响应
[Object, Object, route: "horse", getRestangularUrl: function, getRequestedUrl: function, addRestangularMethod: function, clone: function…]
编辑 2
我也试过这个 - https://github.com/mgonto/restangular#using-values-directly-in-templates
$scope.horse = Restangular.all('horse').getList().$object;
这只会导致输出一个空数组。我还尝试删除 setResponseInterceptor
并修改 api 的结构以直接生成没有元数据(错误等)的数据数组,没有乐趣:(
假设您显示为 "API response" 的内容是从控制器中的 console.log 输出的内容,看来您需要做的就是将示波器模型设置为 属性 "horse" 在这样的响应数据中:
$scope.horse = horse.horse;
由于读起来很奇怪,您应该将 .then 回调的参数名称更改为数据,这将是一个更加不可知的标准参数名称。如果你做了那个改变,你可以从你的回调中将你的马数据设置到你的范围模型,如下所示:
$scope.horse = data.horse;
如果我误解了你的问题,请告诉我。希望这对您有所帮助。
数据似乎正在通过。我注意到你在使用类固醇,你检查过标记而不仅仅是控制台吗?
确保将范围微调器设置为 false,以确保在数据通过时隐藏微调器。
$scope.ShowSpinner = false;