0

I want to trigger/call the callkhs from LihatKhsController to KhsController:

function KhsController($scope, $rootScope){
     $scope.$emit('callkhs', {param:'test'});
}

and I try to get parameter from KhsController to LihatController:

function LihatKhsController($scope, $rootScope){
    $scope.$on("callkhs", function(event, data){
      console.log('halo callkhs', data.param); //not show
    });
}

please help me to solve this.

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Ahmad Sob
  • 9
  • 3

3 Answers3

2

If you use $scope.$emit, it'll notify only it's parent scope.

Like this

<div ng-contoller="LihatKhsController">
 <div ng-controller="KhsController">
 </div>
</div>

JS

function KhsController($scope, $rootScope){
     $scope.$emit('callkhs', {param:'test'});
}
function LihatKhsController($scope, $rootScope){
    $scope.$on("callkhs", function(event, data){
      console.log('halo callkhs', data.param); //not show
    });
}

Here KhsController will trigger LihatKhsController as LihatKhsController is prent of KhsController .

If you use $scope.$broadcast , it'll notify it's child

Like this

<div ng-contoller="KhsController">
 <div ng-controller="LihatKhsController">
 </div>
</div>

JS

function KhsController($scope, $rootScope){
     $scope.$emit('callkhs', {param:'test'});
}
function LihatKhsController($scope, $rootScope){
    $scope.$on("callkhs", function(event, data){
      console.log('halo callkhs', data.param); //not show
    });
}

Here KhsController will trigger LihatKhsController as KhsController is parent of LihatKhsController.

Both of them won't consider siblings controller Like

<div ng-contoller="KhsController">
</div>
 <div ng-controller="LihatKhsController">
 </div>

$emit and $broadcast is slightly different in rootScope.

If you use $rootScope.$emit, it'll only notify on $rootScope.$on.

If you use $rootScope.$broadcast,it'll not only notify on $rootScope.$on but also all $scope.$on.

Like this

function KhsController($scope, $rootScope){
     $rootScope.$broadcast('callkhs', {param:'test'});
}

function LihatKhsController($scope, $rootScope){
    $scope.$on("callkhs", function(event, data){
      console.log('halo callkhs', data.param);
    });
}
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
0

Have you tried redux? It's a modern way to write applications that behave consistently. There is an redux angular bindings.

stevemao
  • 1,423
  • 1
  • 16
  • 29
0

The other answers that use $rootScope.$broadcast and the associated $rootScope.$on will work, but I prefer using $rootScope.$emit in this situation. Considering you are listening from $rootScope, it doesn't make since to propagate the message down the entire scope tree. With $rootScope.$emit and $rootScope.$on, it's essentially a direct message. The performance benefit might be negligible but I see no downside to this approach.

var app = angular.module('khsApp', []);

app.controller("KhsController", function ($scope, $rootScope) {
  $scope.sendMessage = function() {
    $rootScope.$emit('message', 'message sent');
  }
});

app.controller("LihatKhsController", function ($scope, $rootScope) {
  $rootScope.$on("message", function($event, data) {
    $scope.received = data;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>

<div ng-app="khsApp">
  <form ng-controller="KhsController">
    <button ng-click="sendMessage()">Send</button>
  </form>
  <div ng-controller="LihatKhsController">
   <span ng-bind="received"></span>
  </div>
</div>
tylerwal
  • 1,858
  • 2
  • 15
  • 20