angularjs - Access prototypal $scope functions in controller's constructor function -


most of angular new "styles" guide replace anonymous controller functions named constructor functions right angular way (pattern), though documentation not conforming.

e.g:

var testctrl = function(){     this.prop = 'prop';     .... }  angular.module('app').controller('test', testctrl); 

i understand concept sake of debug-ability , memory savings.

my question:

if before listen event subscribing $scope.$on() method:

angular.module( 'app' ).controller( 'test', [ '$scope', function ( $scope ) {     $scope.$on('event', function (event, params) {         ...     }); }]); 

how can in constructor function?

and there catch! injecting $scope in constructor function , subscribing event on $scope not execute callback in constructor function scope.

actually, none of examples have named constructor function, different style register controller.

you use named contructor function sake of debug-ability in both styles, this:

function testctrl() {   this.prop = 'prop';   .... }  angular.module('app').controller('test', testctrl); 

or this:

angular.module( 'app' ).controller( 'test', [ '$scope', function testctrl( $scope ) {   $scope.$on('event', function (event, params) {     ...   }); }]); 

to subscribe events in first style, this:

function testctrl($scope) {   this.prop = 'prop';    $scope.$on('event', function (event, params) {     ...   }); }  angular.module('app').controller('test', ['$scope', testctrl]); 

hope helps.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -