I think I have a syntax error in my code, but I am not entirely sure. Basically when I call a function on the service, it returns the code rather than the return value of the function.
This is the service ...
(function (){
'use strict';
var products = function($http){
var getProducts = function () {
return $http.get(getDataUrl)
.then(function (response) {
return response.data;
});
};
//Make functions public
return {
getProducts: getProducts
};
};
var module = angular.module("shop");
module.factory("products", products);
module.constant("getDataUrl", "xxx")
}());
This is the controller than invokes the product service ...
(function () {
'use strict';
angular
.module("shop")
.controller("ProductController", ProductController);
function ProductController($scope, cart, $routeParams, products) {
// $scope.product = products.getProducts;
// console.log($scope.product);
var productId = getProductIdFromUrl();
console.log("productId: " + productId);
console.log("products: " + products.getProducts);
function getProductIdFromUrl(){
return $routeParams.productId;
}
}
})();
Now rather than returning the JSON data that I am expecting, the console.log is just printing the the functions code. Anyone any ideas?
The console.log prints the following ...
products: function () {
return $http.get(getDataUrl)
.then(function (response) {
return response.data;
});
}