vm.$on undefined error in angularjs -


i following best practice style guide angular development , can't seem 1 part working cleanly.

best practice style guide: https://github.com/johnpapa/angular-styleguide

it recommends declare controllers following:

angular     .module('app')     .controller('feedcontroller', feedcontroller);  function feedcontroller(){    var vm = this; //my $scope variable } 

the problem trying use $on add event listener gives me undefined error vm.$on. temporary solution found inject $scope controller following:

feedcontroller.$inject = ['apiservice', '$scope']; 

and call $scope.$on works feel inconsistent now. there way still use vm in clean way.

you can see full controller here https://github.com/bliitzkrieg/trailerfeed/blob/master/app/components/feed/feedcontroller.js

this/vm refers instance of controller, not scope associated controller.

events exist on scopes, use event system correct inject $scope reference controller's scope $on available.

angular     .module('app')     .controller('feedcontroller', feedcontroller);  function feedcontroller($scope){    var vm = this; // instance of controller    $scope.on(...) // controller's scope } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -