Reputation: 36189
I put the init()
function outside of the return
of my directive :
app.directive('myDirective', function()
{
return {
restrict: 'C',
controller: function($scope)
{
init($scope);
}
}
function init($scope)
{
$scope.params = [];
// Other initializations
}
});
Is this bad practice or a bad idea? I just don't like to have the init()
inside the controller
since I feel that things inside the controller are meant to be reused and run continuously.
One of my reasoning for putting it at the end is that initialization happens once, and I want to place it at the very bottom of the code, where it doesn't bother me, and doesn't steal space from my other code that I spend more time on.
What do you think/suggest?
Upvotes: 2
Views: 1616
Reputation: 1676
I've seen patterns similar to yours many times, don't worry too much about it. The only thing I would change is moving the initialization to the link
function; the controller in directives is used to expose an API to other directive (used through require
), so I wouldn't mix it with other stuff. I try to keep the API/communication part clean and initialize/bind stuff in the link
function.
Upvotes: 4