I've created a simple directive that displays sort column headers for a <table>
I'm creating.
ngGrid.directive("sortColumn", function() {
return {
restrict: "E",
replace: true,
transclude: true,
scope: {
sortby: "@",
onsort: "="
},
template: "<span><a href='#' ng-click='sort()' ng-transclude></a></span>",
link: function(scope, element, attrs) {
scope.sort = function () {
// I want to call CONTROLLER.onSort here, but how do I access the controller scope?...
scope.controllerOnSort(scope.sortby);
};
}
};
});
Here's an example of some table headers being created:
<table id="mainGrid" ng-controller="GridCtrl>
<thead>
<tr>
<th><sort-column sortby="Name">Name</sort-column></th>
<th><sort-column sortby="DateCreated">Date Created</sort-column></th>
<th>Hi</th>
</tr>
</thead>
So when the sort column is clicked I want to fire the onControllerSort function on my grid controller.. but I'm stuck! So far the only way I've been able to do this is for each <sort-column>
, add attributes for the "onSort" and reference those in the directive:
<sort-column onSort="controllerOnSort" sortby="Name">Name</sort-column>
But that's not very nice since I ALWAYS want to call controllerOnSort, so plumbing it in for every directive is a bit ugly. How can I do this within the directive without requiring unnecesary markup in my HTML? Both the directive and controller are defined within the same module if that helps.
See details here.
Here is a answer to a similar question, which shows how to pass argument in the callback function from the directive code.
Call me crazy, but it seems easier to just get the controller from the element via the inbuilt method for that, rather than fiddling with
require
:http://plnkr.co/edit/gY4rP0?p=preview
In your directive require the
ngController
and modify the link function as:What you get as
ngCtrl
is your controller,GridCtrl
. You dont get its scope though; you would have to do something in the lines of:Call it from the link function simply as:
Do note that this require will get the first parent
ngController
. If there is another controller specified betweenGridCtrl
and the directive, you will get that one.A fiddle that demonstrates the principle (a directive accessing a parent
ng-controller
with methods): http://jsfiddle.net/NAfm5/1/People fear that this solution may introduce unwanted tight coupling. If this is indeed a concern, it can be addressed as:
Create a directive that will be side-by-side with the controller, lets call it
master
:This directive references the desired method of the controller (thus: decoupling).
The child directive (
sort-column
in your case) requires themaster
directive:Using the
$parse
service the specified method can be called from a member method of the master controller. See updated fiddle implementing this principle: http://jsfiddle.net/NAfm5/3/There is another way to do this, although given my relative lack of experience I can't speak for the fitness of such a solution. I will pass it along anyhow just for informational purposes.
In your column, you create a scope variable attribute:
Then in your controller you define the scope variable:
Then add your sort function in controller:
Then in your markup wire up ng-click:
Then in your directive you add the sortby attribute to directive scope:
And in your "link:" function add a $watch:
The beauty of this approach IMO is that your directive is decoupled completely, you don't need to call back to onSort from the directive because you don't really leave onSort in the controller during that part of the execution path.
If you needed to tell your controller to wait for the sort to finish you could define an event in the controller:
Then in your directive simply emit the event then the process is done:
There's other ways to do that, and this kind of adds some tight-ish coupling because your controller has to listen for. and your directive has to emit a specific even... but that may not be an issue for you since they are closely related anyhow.
Create a second directive as a wrapper:
Then you can just reference the function to call once in the outer directive:
In the "sortColumn" directive you can then call that referenced function by calling
See this fiddle for a working example: http://jsfiddle.net/wZrjQ/1/
Of course if you don't care about having hardcoded dependencies, you could also stay with one directive and just call the function on the parent scope (that would then be the controller in question) through
I have another fiddle showing this: http://jsfiddle.net/wZrjQ/2
This solution would have the same effect (with the same criticism in regard to hard-coupling) as the solution in the other answer (https://stackoverflow.com/a/19385937/2572897) but is at least somewhat easier than that solution. If you couple hard anyway, i don't think there is a point in referencing the controller as it would most likely be available at $scope.$parent all the time (but beware of other elements setting up a scope).
I would go for the first solution, though. It adds some little markup but solves the problem and maintains a clean separation. Also you could be sure that $scope.$parent matches the outer directive if you use the second directive as a direct wrapper.