Access to parent controller methods from directive?

I have a question with some page about finding elements.

Let's say I have:

  • Controller with result elements to display
  • Service that runs search queries and interacts with the controller to update the result items for display
  • Directive , which has a form with all inputs of the search fields.

One of the buttons is the "View More Items" button. It should only be displayed if the response I get from the request tells me that there are more items to view.

I made a function inside the service that checks this property and returns a boolean to the directivewhich uses it with ng-show on the button to show or hide it.

The problem is that I do not know how to interact with the controller in this area.

If the directive calls one service method , and this method must somehow interact with the controller?

Before that, I wrapped the directive with a form tag (outside the scope of the directive), and then I could use ng-submit to perform some actions on the controller to call the service . Like this:

<form ng-submit="myController.submitSearch">
    <search-options-directive></search-options-directive>
    <button type="submit">
</form>

But now what I'm trying to do is put the form and buttons inside

<search-options-directive></search-options-directive>

So now I do not have direct access to controller methods .

How do I approach this?

+2
1

: " ". , :

return {
   scope: {
     myFavoriteMethod:'&'
   }
}

on:

<search-options-directive my-favorite-method="myMethodOnTheParentController"></search-options-directive>

html:

 ng-click="myFavoriteMethod()"

ng-submit, onsubmit ( ), ng-click, .

0

Source: https://habr.com/ru/post/1583812/


All Articles