In my MEAN Stack application when I want to return a static response from a service, I can get the result on controller, and when I want my response with a $http request its not returning the valid data.
Here is my service page-
'use strict';
angular.module('products').factory('ProductSharedService', function($rootScope, $http) {
var sharedService = {
getProductList: function (theScope) {
// Working
theScope.productList = {'testkey':'testvalue', 'demokey':'demovalue'};
// Not working
$http.get('/products').then(function(response) {
theScope.productList = response;
});
}
};
return sharedService;
});
And here is the controller page -
'use strict';
angular.module('core').controller('InviteController', ['$scope', 'ProductSharedService', '$stateParams', 'Authentication', '$http',
function($scope, ProductSharedService, $stateParams, Authentication, $http) {
$scope.getMembers = function() {
$scope.productList = {};
ProductSharedService.getProductList($scope); // Get all product from "ProductSharedService" service
console.log(JSON.stringify($scope.productList));
console.log($scope.productList);
};
Here is the result with static data. You can see the returned object.
And the result with $http request. Now there is nothing.
The $http request will give me the following, if I place it on controller -
{ "_id" : ObjectId("5537cdef3c3a11cd3baa90f8"), "artist" : "test artistname", "title" : "test title", "dimensions" : "140x145", "medium" : "test medium", "price" : 140, "provenance" : "test"}
I have looked at the other questions like this one related to mine, but those did not help me with the current problem.