Hi,
Sometimes it can be hard to reuse current directives, because there is
no simple way to make a custom directive delegate stuff to another.
The directive's controller is supposed to be shared and adapted in
different scenarios, unfortunately there is no way to ask directive X
to get controller of directive Y when X is at the same node (or child
node).
Fortunately, most of the time one can skip the problem by adjusting a
little bit the concept. In your case, I would write a directive and
apply it on the text input field, so you could get the controller of
the input directive. Example:
<input type=text ng-model=something inc-buttons><button
up>+</button><button down>-</button>
OK, now we have to write a inc-button directive. This directive is
operating on the input element, so you can ask for controller. You
also want to attach actions to two buttons. Since you know the buttons
are next to the input, you can find them easily:
elem.next('button[up]').click(function() {
scope.$apply(function() { ..... });
});
elem.next('button[down]').click(function() {
scope.$apply(function() { ..... });
});
Another option would be to create a container element with directive
with controller like this:
<div inc-buttons>
<input type=text ng-model=something inc-button><button
inc="+">+</button><button inc="-">-</button>
</div>
Now, in you have to write
inc-buttons directive with a directive controller,
inc-button and inc directives which will ask for controller created by
"inc-buttons". This controller would act as a common communication
channel for between inc-button and inc directives.
I think the first approach requires less code but forces you to assume
where are the + and - buttons are.
How do you find those approaches?
Regards,
Witold Szczerba