I decided to write a simple runnable example to try and express my concern:
index.html
<section ng-app="App" ng-clock>
<div ng-controller="ShopController as shop">
<prize-directive ng-repeat="prize in prizes"></prize-directive>
</div>
</section>
Inside my shopController
I have set a static prize list just for the purpose of keeping it simple.
ShopController.js
angular.module('app.controllers').controller('ShopController',
['PrizeService', '$scope', function (PrizeService, $scope) {
$scope.prizes = [
{name: 'First Prize', cost: 10,
image: 'upload/prize/image1.png', note: 'This is a note'},
{name: 'Second Prize', cost: 20,
image: 'upload/prize/image2.png', note: 'This is a note'},
{name: 'Third Prize', cost: 30,
image: 'upload/prize/image3.png', note: 'This is a note'}
];
$scope.reclaim = function (prize) {
console.log(prize);
};
}]);
In the original code I use the PrizeService
to load a list of available prizes, but back to my main problem:
PrizeDirective
angular.module('app.directives').directive('prizeDirective', function () {
return {
restrict: 'E',
templateUrl: 'js/directives/PrizeDirective.html'
};
});
HTML:
<div>
<img ng-src="{{Endpoint + prize.image}}" />
<h3>{{prize.name}}</h3>
<span>Pontos: {{prize.cost}}</span>
<p>{{prize.note}}</p>
</div>
<reclaim-directive></reclaim-directive>
Here in the HTML portion of the directive, I'm using a variable name that was defined in the index.html. Although it bothers me, I'm willing to let it go because the variable name was defined inside the ng-repeat
code, which is inside the prize-directive
directive. But when we go to the reclaim-directive
, things get a little bit more heated:
angular.module('app.directives').directive('reclaimDirective', function () {
return {
restrict: 'E',
template: '<button ng-click="reclaim(prize)">Resgatar</button>'
};
});
Note: I'm actually using the templateUrl
here, but again, I'm simplifying it just to show my point.
Now I'm using a function name defined inside ShopController
in the Reclaim Directive. Differently from OOP, this is not really an inheritance of an abstract method which even the IDE can tell you that something is wrong and, to me, it seems like a bad practice.
I would love to hear people's thoughts on this approach and if there is anything that I'm missing that could help clean up this code.