最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - angular $scope.$on is not working - Stack Overflow

programmeradmin5浏览0评论

I am banging my head with this. My controller structure is appController at the top, then a mapController. all other controllers are nested and are parallel of each other. they are being instantiated with the ui-router ui-view state change.

In my map controller I have this inside a function

$scope.$broadcast('selectedIdsForExport', selectedIds);

this works. the $on function is inside a chartController that has just been instantiated with a state change.

$scope.$on('selectedIdsForExport', function (event, selectedIds) {

this is being hit from the broadcast. but it does not enter the function.it skips over it.this is where i am lost. I do have this $on function inside a $watch function, I thought that could be a issue but if it was a issue the $on function would not be hit? Am I wrong about that?

how can i trouble shoot this?

note that inside my mapController I have 2 functions that both have the same $broadcast function. This should not be a issue? also I two chartControllers that have the same $on function. But if that was a issue the breakpoint would not catch on the $on function that I am trying to enter. is that correct? here is a couple of screenshots that might help. on my page load/refresh this is the pattern. this works correctly

when I click on the tab to switch charts, this is the pattern.

this is where it skips over the $on function

I am banging my head with this. My controller structure is appController at the top, then a mapController. all other controllers are nested and are parallel of each other. they are being instantiated with the ui-router ui-view state change.

In my map controller I have this inside a function

$scope.$broadcast('selectedIdsForExport', selectedIds);

this works. the $on function is inside a chartController that has just been instantiated with a state change.

$scope.$on('selectedIdsForExport', function (event, selectedIds) {

this is being hit from the broadcast. but it does not enter the function.it skips over it.this is where i am lost. I do have this $on function inside a $watch function, I thought that could be a issue but if it was a issue the $on function would not be hit? Am I wrong about that?

how can i trouble shoot this?

note that inside my mapController I have 2 functions that both have the same $broadcast function. This should not be a issue? also I two chartControllers that have the same $on function. But if that was a issue the breakpoint would not catch on the $on function that I am trying to enter. is that correct? here is a couple of screenshots that might help. on my page load/refresh this is the pattern. this works correctly

when I click on the tab to switch charts, this is the pattern.

this is where it skips over the $on function

Share Improve this question edited Aug 21, 2015 at 18:20 texas697 asked Aug 21, 2015 at 18:14 texas697texas697 6,47719 gold badges70 silver badges136 bronze badges 5
  • Can you post a fiddle or a plunk illustrating the problem? I would also remove the $on from inside the $watch just from a pure code quality standpoint. – sma Commented Aug 21, 2015 at 18:20
  • 1 Why would you include the listener in a $watcher ? – malifa Commented Aug 21, 2015 at 18:20
  • What he^ said. Every time your value changes you are generating the same listener over and over again. Memory leaks. – Rorschach120 Commented Aug 21, 2015 at 18:23
  • Why not include the relevant code and controller hierarchy instead of trying to explain in words? Why do we need to tease out the true meaning of apparently contradictory statements of "controllers are nested and are parallel", or "hit from the broadcast. but it does not enter the function.it skips over it"? – New Dev Commented Aug 21, 2015 at 18:24
  • I was handed this project. its not setup the best way. but i need to get it going as is. – texas697 Commented Aug 21, 2015 at 18:31
Add a ment  | 

1 Answer 1

Reset to default 2

I think this is the issue of the parent and child priority on calling them $on and $broadcast.

please read the following stackoverflow question.

Working with $scope.$emit and .$on

发布评论

评论列表(0)

  1. 暂无评论