0

I have two views and its resp controller. Say view1.html and its controller firstCntrl and view2.html and its controller secndCntrl.

In view1.html:

There is Link2 which will be redirected to view2.html if it is clicked.

<a href="#/view1" ng-click="emit()">Link1</a>  
<a href="#/view2" ng-click="emit()">Link2</a>

in ng-click I am calling emit function where I defined $emit as firstCntrl.js

 app.controller("firstCntrl", function ($scope) {
    $scope.emit = function() {
      $scope.$emit('send', { message: 'true' });
    };
 });

I want to send true from here and catch it in $on.

In view2.html

There are two div. say div one and div two.

<div ng-show="one">Hello</div>
<div ng-show="two">World</div>

What I want that If user clicks on first link then it should show the first div of view2.html

Or

if user clicks on second link then it should show sencod div of view2.html View two have his own Secnd controller. I am struck here. Help me in this How to do this. Thanking you in anticipation.

digit
  • 4,479
  • 3
  • 24
  • 43
Mohammed
  • 648
  • 2
  • 12
  • 32

1 Answers1

0

I think you don't need emit here since firstCtrl and secondCtrl is a separated controller. In case there is no parent-child relation between your scopes you can inject $rootScope into the controller and broadcast the event to all child scopes.

View1.html

<a href="#/view1" ng-click="broadcastMsg(1)">Link1</a>  
<a href="#/view2" ng-click="broadcastMsg(2)">Link2</a>

firstCtrl

app.controller("firstCntrl", function ($scope, $rootScope) {
    $scope.broadcastMsg = function(id) {
      if (id == 1) 
          $rootScope.$broadcast('send', 'One');
      else 
          $rootScope.$broadcast('send', 'Two');
    };
 });

View2.html

<div ng-show="showOne">Hello</div>
<div ng-show="!showOne">World</div>

parentCntrl

Use $scope.on instead of $rootScope.on to prevent memory leak issue.

app.controller("parentCntrl", function ($scope, $rootScope) {
      $scope.$on('send', function (evt, message) {
            if (message == 'One') 
                $scope.showOne = true;
            else
                $scope.showOne = false;
      });
 });

secondCtrl

app.controller("secondCntrl", function ($scope) {
      $scope.showOne = $scope.$parent.showOne;
 });
digit
  • 4,479
  • 3
  • 24
  • 43
  • one thing I noticed that if I clikcking on msg1 or msg 2 but it showing only World. no Hello – Mohammed Jan 14 '17 at 06:24
  • I m facing one bug, that $broadcast is not working on single click. I have to click two times for that. @digit – Mohammed Jan 16 '17 at 06:47
  • It's not bug i think. It's the delay of execution. Can you debug the code by putting console.log('enter') in rootscope.on. What u will get after clicking at the 1st time? – digit Jan 16 '17 at 07:13
  • No. Im getting it after clicking it on 2nd time @digit – Mohammed Jan 16 '17 at 07:29
  • I have put my $broadcast at then end of controller. So whats happening basically is when $broadcast starts it redirected to start of controller. Ans on second click its searching for $on – Mohammed Jan 16 '17 at 07:34
  • Do you have parent controller? – digit Jan 16 '17 at 07:38
  • yes my header controller is home controller and its included through ng-include in other view @digit – Mohammed Jan 16 '17 at 07:39
  • Try put rootscope.on in parent controller. – digit Jan 16 '17 at 09:41
  • its stop working if I am using $rootScope.$on @digit – Mohammed Jan 16 '17 at 11:19
  • I know the issue is. I guess the other controller which is supposed to receive the event is not yet instatiated when you are $broadcasting the event. – digit Jan 16 '17 at 13:01
  • put rootscope.on in parent controller . Let me update my answer – digit Jan 16 '17 at 15:31
  • I implemented this. frstCntrl is my parent controller so I posted $broadcast and $on on same controller and in second controller I write last code block which you edited yesterday. But by this method view is not loading although url changing. – Mohammed Jan 17 '17 at 07:18