0
我正在学习AngularJS。 我的服务:AngularJS资源
services.factory('Model', ['$resource',
function ($resource) {
return $resource('model/:id', {}, {});
}
]);
services.factory('Department', ['$resource',
function ($resource) {
return $resource('department/:id', {}, {});
}
]);
services.factory('Price', ['$resource',
function ($resource) {
return $resource('price/:id', {}, {});
}
]);
我的控制器:
controllers.controller('SafeNewCtrl', ['$scope', '$location', 'Safe', 'Model', 'Department', 'Price',
function ($scope, $location, Safe, Model, Department, Price) {
$scope.models = Model.query();
$scope.departments =Department.query();
$scope.prices = Price.query();
// It doesn't work. console.log($scope.models[0] 'and other') = undefined.
$scope.safe = {model: $scope.models[0], department: $scope.departments[0], price: $scope.prices[0]};
$scope.save = function() {
var safe = new Safe($scope.safe);
safe.$save(function() {
$location.path('list/f');
})
}
}
]);
我有资源阵列每次查询后()。 如何获得正常的JSON作为数组和数组的第一个对象设置为$ scope.safe?
电话是异步如此,因为它们没有获得您需要使用响应处理程序来处理这个问题,因为不幸的是$资源是如何工作的,这是不完全微不足道的,你不能立即访问结果...在这种情况下使用$ http实际上更容易一些,因为你得到了一个承诺,并可以使用$ q.all()来处理所有结果,然后我会尝试写出一些东西。 – shaunhusain
啊只是检查了文档看起来你可以得到原来的承诺,所以会在下面猜测。 – shaunhusain