I use restangular
to connect to REST API. I define a ProductService.js
with this code:
'use strict';
angular.module('app').service('ProductService', function($rootScope, Restangular) {
// Build collection /product URL
var _productService = Restangular.all('product');
this.list = function() {
// GET /api/product
return _productService.getList();
}
this.create = function(product) {
// POST /api/product/:id
_productService.post(product).then(function() {
$rootScope.$broadcast('product.create');
});
}
});
and I use this service in ProductController.js
:
"use strict";
app.controller('ProductController',function($scope,$http,Restangular,ProductService){
// get products
$scope.products = ProductService.list();
// create product
$scope.create = function(product) {
ProductService.create(product);
};
// Event Listeners
$scope.$on('product.create', function(product) {
$scope.products = ProductService.list();
console.log('product create');
});
});
and anythings is okay when page load and after that when create a product show me this page:
I solved problem and add $object
to list function in ProductService.js
:
this.list = function() {
// GET /api/product
return _productService.getList().$object;
}