我有一個RESTful
應用程序Laravel 4
和Angular JS
。
在我Laravel控制器,
public function index() {
$careers = Career::paginate($limit = 10);
return Response::json(array(
'status' => 'success',
'message' => 'Careers successfully loaded!',
'careers' => $careers->toArray()),
200
);
}
和角腳本,
var app = angular.module('myApp', ['ngResource']);
app.factory('Data', function(){
return {
root_path: "<?php echo Request::root(); ?>/"
};
});
app.factory('Career', [ '$resource', 'Data', function($resource, Data) {
return $resource(Data.root_path + 'api/v1/careers/:id', { id: '@id'});
}]);
function CareerCtrl($scope, $http, Data, Career) {
$scope.init = function() {
$scope.careers = Career.query();
};
}
在這裏,我有點糊塗了處理響應數據分配給範圍變量,現在我得到空數組[]
in $scope.careers
。而且我怎麼能處理成功和錯誤顯示一些信息,如按正常$http service
,
$scope.init = function() {
Data.showLoading(); // loading progress
$http({method: 'GET', url: Data.root_path + 'api/v1/careers'}).
success(function(data, status, headers, config) {
Data.hideLoading();
$scope.careers = data.careers.data;
}).
error(function(data, status, headers, config) {
if(data.error.hasOwnProperty('message')) {
errorNotification(data.error.message);
} else {
errorNotification();
}
$scope.careers = [];
});
};
使用$資源查看控制檯我的要求。
謝謝,你節省了我的時間... – devo
同樣在這裏一直在掙扎小時 – Awena