ng-show="cart.getItems().length < 1" ng-hide="cart.getItems().length > 0">
class="imgShopCart" ng-src="../resources/images/shoppingcartClear.png">
.service('cartService', function () {
let items = [];
return {
getItems: function () {
return items;
},
addArticle: function (article) {
items.push(article);
},
removeArticle: function ($index) {
items.splice($index, 1);
},
removeAll: function () {
items = [];
},
sum: function () {
return items.reduce(function (total, article) {
return total + article.price1
}, 0);
}
};
})
.controller('ArticlesCtrl', function ($scope, $http, cartService) {
$scope.cart = cartService;
$http.get('articles.json').then(function (articlesResponse) {
$scope.articles = articlesResponse.data;
});
})
.controller('CartCtrl', function ($scope, cartService) {
$scope.cart = cartService;
})
|
<body>
<div ng-controller="ArticlesCtrl">
<div ng-show="cartlength > 0">
<img class="imgShopCart" ng-src="images/{{cartlength}}.png" />
<div>Returned Item length: {{cartlength}}</div> // image name is based on the number of items available in cart.
</div>
</div>
<script>
angular.module('CartCtrl', [])
.service('cartService', function () {
let items = ['1', '2', '3', '4'];
return {
getItems: function () {
return items;
},
addArticle: function (article) {
items.push(article);
},
removeArticle: function ($index) {
items.splice($index, 1);
},
removeAll: function () {
items = [];
},
sum: function () {
return items.reduce(function (total, article) {
return total + article.price1
}, 0);
}
}
})
.controller('ArticlesCtrl', function ($scope, cartService) {
$scope.data = cartService.getItems();
$scope.cartlength = cartService.getItems().length;
});
</script>
</body> |