Hello,
I'm having issues with AngularJS services. It looks like service is getting
initialized every time i refresh the view or navigate to diferent view. I
was thinking the services are singleton and initialized at the starting and
not every time view loaded Here is code i used angular .js. I'm using
angular js with MVC 5
(function () {
var app = angular.module('MyStore', []).service('cartService', function () {
console.log('inside service');
var cartItems = [];
this.addCartItem = function (newitem) {
cartItems.push(newitem);
console.log(cartItems);
};
this.getShoppingCartItems = function () {
console.log(cartItems);
return cartItems;
};
});
app.controller('CartController', ['$scope', 'cartService', function ($scope,
cartService) {
$scope.cartItems = cartService.getShoppingCartItems();
}]);
app.controller('ProductController', ['$scope', 'cartService', function ($scope,
cartService) {
$scope.cartItems = cartService.getShoppingCartItems();
$scope.addToShoppingCart = function (id, name, price, qty) {
var newitem = { id: id, name: name, price: price, quantity: qty };
cartService.addCartItem(newitem);
};
$scope.products = [];
$scope.products.push({ id: 1, name: 'G1', price: 2.85, description:
'description for G1' });
$scope.products.push({ id: 2, name: 'G2', price: 5.85, description:
'description for G2' });
$scope.products.push({ id: 3, name: 'G3', price: 5.85, description:
'description for G3' });
}]);
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.