I have a shopping cart component in my app. It lives in a controller. The controller is instantiated in a partial which is loaded to the page on demand (icon in menu bar).
app.controller('CartCtrl', function($scope, $http, storage) {
$scope.options = {
freeShipmentFrom: 180,
shipmentCosts: 6
...
};
$scope.init = (function(){ ... }());
$scope.addItem = function(){ ... };
...
// more methods
...
});
Now I have to provide functionality outside of this partial/controller, even if that is not loaded to the page. I use directives for that, so for example I have a directive for adding items and for displaying the number of items in cart.
<button add-to-cart="productId">Add to cart</button>
How would you structure/design this component concerning best practices? Adding the "add-to-cart"-logic to the directive? Define a service outside of the partial mentioned above and accessing it from the directive and the controller of the partial?
Looking forward to read your thoughts on that!