2

I have followed this post

I have a parent component which contains (and it's called) :

$scope.$broadcast('searchNoFilter');

and a child component which contains (and it's called) :

$scope.$on('searchNoFilter', function(e) {
    self.search();
});

The parent triggers that event, but the child's $on body isn't called.

What am I missing?

Morteza Asadi
  • 1,819
  • 2
  • 22
  • 39
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • the're not enough information to really tell, post some more code, make an MVCE/plunk. – svarog Aug 19 '17 at 21:24
  • @georgeawg I think you're right. but how would model help? the child would pool with watch on that model? – Elad Benda Aug 20 '17 at 06:40
  • Most likely the event being broadcast happens before the listener is attached to the child component scope. It is best to avoid broadcasting events. Have events in parent elements modify Model values in the parent element. Use one-way `<` binding, to bind parent Model values to child scope. Model values persist and are available regardless of when a child component is instantiated. Events are transient and can be missed. – georgeawg Aug 20 '17 at 10:32

2 Answers2

1

Broadcast and emit works in different ways . Broadcast sends events towards child scopes while emit towards parent . Most probably you missed that . If you broadcast from rootScope this should probably work .

See details here also here

Abdullah Al Noman
  • 2,817
  • 4
  • 20
  • 35
0

Most likely the event being broadcast happens before the listener is attached to the child component scope. It is best to avoid broadcasting events. Have events in parent elements modify Model values in the parent element. Use one-way < binding, to bind parent Model values to child component scope. Model values persist and are available regardless of when a child component is instantiated. Events are transient and can be missed.

georgeawg
  • 48,608
  • 13
  • 72
  • 95