0

In My Application I have two controllers .

Controller 1 | Controller 2


  1. From Controller 1 I am opening a menu on a button click and in a function defined a property say, this.isTelephoneMenuOpen = true;

Result : I am getting menu opened With two option edit and Remove.

2.From Controller 2 on click of edit I am opening a modal-overlay- working fine.

Issue :But not being able to hide the earlier opened menu.

How to use this property in another controller to hide the menu after modal gets opened?

Mistalis
  • 17,793
  • 13
  • 73
  • 97
  • Possible duplicate of [Creating common controller functions](http://stackoverflow.com/questions/11324202/creating-common-controller-functions) – Mistalis Dec 14 '16 at 10:53

2 Answers2

0

Define the property as $rootScope as every application has a single root scope.

In Controller 1 - $rootScope.isTelephoneMenuOpen = true.

In Controller 2 - Set it to false after opening the menu $rootScope.isTelephoneMenuOpen = false.

abhit
  • 973
  • 3
  • 17
  • 40
  • @lakshaya : Thanks Lakshya. Initially,$rootScope.isTelephoneMenuOpen = false ----->In Controller 1 Then, $rootScope.isTelephoneMenuOpen = true ----->In Controller 1 Result: opens the menu Need to make , $rootScope.isTelephoneMenuOpen = false----->In Controller 2 But Its taking false as intial value...so how to make it false again in controller 2 ? – pourushsingh gaur Dec 14 '16 at 12:01
  • u don't have to set `$rootScope.isTelephoneMenuOpen = false` as initial value. you should set it to false at the time when your model is opened and the menu should hide. – abhit Dec 14 '16 at 12:59
  • @lakshya:In My Project , Rootscope is not working :( Menu is not getting open when I put this property to Rootscope – pourushsingh gaur Dec 15 '16 at 05:50
  • have u defined $rootScope in your controller? – abhit Dec 15 '16 at 05:53
  • @lakshya: In First Controller : Getting the value true – pourushsingh gaur Dec 15 '16 at 06:24
0

One of the ways is broadcast event, have a look at the $rootScope.$broadcast().

For example, in the second controller you can broadcast an event

$rootScope.$broadcast('hideMenu', {hide: true})

and then catch it in the first controller

$scope.$on('hideMenu', function (event, data) {
  //code for hiding menu
});
strange
  • 23
  • 5