This is probably going to be a bit complicated, so I will try to keep it as easy as I can. I have a directive:
.directive('configuratorRows', function () {
return {
restrict: 'A',
scope: {
garments: '=configuratorRows',
templateUrl: '&',
colours: '='
},
templateUrl: 'assets/tpl/directives/configuratorRows.tpl.html',
controller: 'ConfiguratorRowsDirectiveController',
link: function (scope, element, attrs, controller) {
// Assign our garments to our controller
controller.garments = scope.garments;
scope.rows = controller.rows;
// Invoke our calculation function
controller.buildRows();
}
};
});
This directive is in charge of working out how many rows of "garments" should be shown in a particular area. On one of my views I invoke it two times like this:
<div configurator-rows="controller.sportswear" colours="controller.colours" ng-if="controller.sportswear.length">
</div>
<div configurator-rows="controller.leisurewear" colours="controller.colours" ng-if="controller.leisurewear.length">
</div>
So far so good, on this page I get a list of different garments based on their type (leisurewear of sportswear) and everything works as it should. The template for this directive looks like this:
<div class="row flex-xs" ng-repeat="row in rows">
<div class="col-md-4 col-sm-6 text-center flex-xs flex-end" ng-repeat="garment in row track by $index">
<div class="kit-template" configurator="garment" colours="colours" designs ng-if="garment"></div>
</div>
</div>
As you can see, there is another directive in here, this just displays an SVG image for each garment in a row. Now, I have another area (on the same view) which lists all garments and displays them slightly differently. The same rules apply, so I would like to use my configuratorRows directive, but the problem is the "template" is different, I want it to look like this:
<div class="row">
<div class="col-xs-4 total-item" ng-repeat="garment in kit.garments">
<div class="row">
<div class="col-xs-4 kit-template" configurator="garment" colours="colours" designs></div>
<div class="col-xs-8">
<h4 class="total-title">{{ garment.title }}</h4>
<p>Quantity: <button class="btn btn-xs btn-primary" type="button" ng-disabled="garment.quantity <= 10" ng-click="modifyQuantity(garment)"><span class="fa fa-minus"></span></button> <span class="text-black">{{ garment.quantity }}</span> <button class="btn btn-xs btn-primary" type="button" ng-click="modifyQuantity(garment, true)"><span class="fa fa-plus"></span></button></p>
</div>
</div>
</div>
</div>
I started down the route of allowing a templateUrl override, but then I realised that the area where I am placing this directive is also a directive and has functions assigned to each bit (check the Quantity buttons).
So my next option was to make the configuratorRows directive without a template and just use ng-transclude instead. The problem with that, is that having more than one configuratorRows in a view seemed to mess around with the scope for each one. So in one I might have 4 rows, but in another I might have 2.
Does anyone know how I can get around this problem elegantly?