0

I'm including various subviews using ng-include, each of which have their own controllers. I want to be able to specify a function that takes a string (coming from a textbox, if you care) and decides on a view by view basis whether it should be visible or not. For example, if I type "tools" into the textbox, I want those views that aren'r related to tools to disappear. Ideally, each view (and its controller) would specify its own filter function.

Can I access the controllers in a view and call one of their functions? I'm assuming it's useful to have the function specified in the view's controller to keep the related code together.

whiterook6
  • 3,270
  • 3
  • 34
  • 77
  • I'd suggest the question you should be asking is how to inject services into controllers, if the function is in a factory, you just have to inject the factory into whatever controller to access the function. Demo example here http://stackoverflow.com/a/25418541/1803298 – cheekybastard Feb 11 '15 at 02:18

1 Answers1

0

You don't need to access sub-controllers to control the behaviors of sub views.

You can make the controller as parent controller, and all controllers for sub views are children of that controller. Then all children controllers can access the scope data of parent controller. Then just use ng-show, ng-hide, ng-if or something else with the data inputted in text input of parent controller.

Mavlarn
  • 3,807
  • 2
  • 37
  • 57
  • So, instead of the parent controller hiding the children, the children watch the filter value and hide themselves? – whiterook6 Feb 11 '15 at 05:37
  • yes. And another solution is to use event broadcast in parent controller and listen the event and update in child controllers. – Mavlarn Feb 13 '15 at 06:22