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

samithaf picture samithaf  路  12Comments

philmerrell picture philmerrell  路  10Comments

nonopolarity picture nonopolarity  路  5Comments

sgbeal picture sgbeal  路  7Comments

robrothedev picture robrothedev  路  6Comments