Howdy y'all,
Recently I implemented a way to communicate from a controller to a
directive using an API object (since I hate dealing with events). Allow me
to quickly demonstrate:
module.controller('MyCtrl', function ($scope) {
$scope.directiveAPI = {};
function doStuffWithDirective () { $scope.directiveAPI.directiveMethod();
}
});
Now in the template:
<my-directive api="directiveAPI"></my-directive>
In the directive:
module.directive('myDirective', function() {
return {
scope: { api: '=' },
link: function (scope) {
scope.api.directiveMethod = function() { ... };
}
};
});
At first, I was all smug and feeling smart about this implementation of
controller-to-directive communication, but the more I thought about it, the
more doubts I had about it being a good design. The first design kink
happened when the controller needs to do something with the directive right
as the controller is instantiated:
module.controller('MyCtrl', function ($scope) {
$scope.directiveAPI = {};
_.defer($scope.directiveAPI.directiveMethod());
});
Notice, that I need to _.defer the execution of my code since the binding
has not happened at that time.
Now my questions for y'all is:
Is this a good design choice for c-to-d communication?
How can I avoid having to _.defer code execution?
In my use case, the controller is driving the interaction with the user,
while the directive handles some very complicated DOM manipulation
($compiles, transcludes, etc).
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.