I wrote my very first directive and I'd like to get some suggestions for improvement. My code - plnkr.
webApp.controller('AppCtrl', function ($scope) {
$scope.expandAdd = function() {
$scope.hiddenAdd = true;
};
$scope.addWeb = function() {
$scope.hiddenAdd = false;
};
});
webApp.directive('enter',function(){
return {
link: function (scope, element, attrs){
scope.hiddenAdd = false; //hides the site details at first
//Invoke expender on the controller on mouse click
element.bind('click', function(){
console.log('clicked');
scope.$apply(attrs.enter);
});
}
}
});
I have three points I think I can improve here but I'm not sure how:
My directive is not modular enough, the line
scope.hiddenAdd = falseis bound tohiddenAdd. Is it possible to make it more flexible for future use? Though it seems two be picking to differentbindingsI think that is because they are both start withhidden.Inside the
controllerI'm doingDOMmanipulation$scope.hiddenAdd = true;&$scope.hiddenAdd = false;from what I've read it all best be insidedirectivesand I can't find the way to make it a puredirective.I read somewhere that it is not recommended to use
$applytoo often, is there a way to avoid usingapplyin my case?