Angular-styleguide: how to call $scope.$broadcast when use vm=this?

Created on 12 Sep 2014  ·  4Comments  ·  Source: johnpapa/angular-styleguide

TypeError: vm.$broadcast is not a function

when publishing and subscribing events using $emit, $broadcast, or $on consider moving these uses to a factory and invoke from the controller.

is there an example?

question

Most helpful comment

You can certainly do that. Part of the advantage of using "controller as" is that the $scope does many things. Controller As allows the data binding aspects to remain separate and then we only inject $scope when we need it for something else.

When I need $scope for something in a controller, I try to first see if I really need it, and if I do I abstract it to a service for tings like broadcast/emit. So in your case I would still abstract it.

All 4 comments

if you need the $scope methods, you need to inject $scope.

So is it ok to use this and $scope in one controller?

You can certainly do that. Part of the advantage of using "controller as" is that the $scope does many things. Controller As allows the data binding aspects to remain separate and then we only inject $scope when we need it for something else.

When I need $scope for something in a controller, I try to first see if I really need it, and if I do I abstract it to a service for tings like broadcast/emit. So in your case I would still abstract it.

Clear! Thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bampakoa picture bampakoa  ·  3Comments

samithaf picture samithaf  ·  12Comments

yosiasz picture yosiasz  ·  7Comments

jansepke picture jansepke  ·  12Comments

Bekt picture Bekt  ·  13Comments