0

I have the following code :

Test.html:

<div>Test</div>
<dta-feed my-list="myList"></dta-feed>

TestCtrl.js

 angular.element(document).ready(function () {
  $scope.loadData();                
 });

 $scope.loadData = function () {
  $rootScope.$broadcast("testEvent");               
 }

dtafeed-directive.js

$scope.$on("testEvent", function () {
  console.log('Testing broadcast event');           
});

Can anyone help me to know h

santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143
  • I will need more context. Can you show us your application configuration? like when or how your directive is being added to the application and when and how the controller is added as well – Dalorzo Jun 08 '20 at 16:12
  • Is your directive ready when you broadcast? – Alexander Jun 08 '20 at 20:11
  • Thanks @Alexander for your response. It is a good question raised by you. But can you please help me to know how to check whether directtive is ready in this case. – santosh kumar patro Jun 09 '20 at 05:08
  • You can debug or log the sequence in which they are instantiated. 1 at the broadcast on the root, 2 in the directive (link lifecycle hook perhaps, can't rembember) – Alexander Jun 09 '20 at 10:13

2 Answers2

0

Would you like to try?

Test.html :

<div>Test</div>
<dta-feed my-list="myList"></dta-feed>

TestCtrl.js

 angular.element(document).ready(function () {
  $scope.loadData();                
 });

 $scope.loadData = function () {
  $rootScope.$broadcast("testEvent");               
 }

dtafeed-directive.js

var ev = $rootScope.$on("testEvent", function () {
  console.log('Testing broadcast event');           
});

$scope.$on('$destroy', function () {
    ev();
});
KiDoo Song
  • 176
  • 5
0

This link helped me to fix the issue Broadcast not received in directive

$timeout(function(){
    $scope.$broadcast('testEvent');
});
santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143