After a few tries, I am now able to access the external service factory (was told it's a good practise to decentralise it) to retrieve product data and display them in my view.
At the moment I am passing the $scope to the function, which seems kind of ugly (especially if you add further CRUD methods).
Is there a better / more appropriate way to achieve this?
Thanks in advance.
At the moment I use it as seen below (which is working):
services.js
angular.module('services', [])
.constant("baseDataUrl", "http://localhost:55451/api/")
.factory('sportsstoreService', function ($http, baseDataUrl) {
var serviceApi = {};
serviceApi.getProducts = function ($scope) {
$http({ method: 'GET', url: baseDataUrl + 'product' }).
success(function (data, status, headers, config) {
$scope.data.products = data;
}).
error(function (data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
};
return serviceApi;
});
sportsstore.js
angular.module("sportsStore")
.controller("sportsStoreCtrl", function ($scope, sportsstoreService) {
$scope.data = {};
sportsstoreService.getProducts($scope);
});
$resourceservice (docs.angularjs.org/api/ngResource/service/$resource). You could return in your sportService factory the new instance of object returned by$resourceand then use it likeSportService.get();