Ho un'applicazione RESTful
con Laravel 4
e Angular JS
.
Nel mio controller laravel,
public function index() {
$careers = Career::paginate($limit = 10);
return Response::json(array(
'status' => 'success',
'message' => 'Careers successfully loaded!',
'careers' => $careers->toArray()),
200
);
}
e lo script angolare,
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();
};
}
Eccomi po 'confuso per gestire i dati di risposta da assegnare alla variabile campo di applicazione, ora sto ottenendo array vuoto []
in $scope.careers
. E anche Come posso gestire il successo e l'errore di mostrare alcuni messaggi come il seguente normale $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 = [];
});
};
Vedi la mia richiesta in console utilizzando $ risorsa.
Grazie, hai salvato il mio tempo ... – devo
stesso qui a lottare per ore – Awena