2

I have a simple app and couple of controllers. In each controller I defined a "init" function. I do this because I want that it can be easy to "refresh" the controller - just call the init function like this: $scope.init().

In other words, something like Page_Load event in .Net (A function that if you defined it in your code it will be firing when the page is load.

I'm trying to use decorator function that run for each controller, but I don't know how to finish this to accomplish the mission.

var myApp = angular.module('myApp',[]);

myApp.decorator('$controller', ['$delegate', function ($delegate) {
    return function (constructor, locals) {
        var controller = $delegate.apply(null, arguments);
        // Never got hear
        if (locals.$scope && locals.$scope.init) {
            console.log('init');
            locals.$scope.init();
        }

        return angular.extend(function () {
            return controller();
        }, controller);
    };
}]);

myApp.controller('ctrl1', function($scope) {
  $scope.init = function() {
  	$scope.name = 'Superhero';
  };
  
  // I don't want to call this
  $scope.init();
});

myApp.controller('ctrl2', function($scope) {
  $scope.init = function() {
  	$scope.name = 'Mario';
  };
  
  // I don't want to call this
  $scope.init();
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="ctrl1">
    Hello, {{name}}!
  </div>
  <div ng-controller="ctrl2">
    My name is, {{name}}!
  </div>
</div>

1 Answer 1

3

You add this function to scope, inside controller. So it would be added in this line

return angular.extend(function () {
    return controller(); //here
}, controller);

you can simple save this value, can call it, like

return angular.extend(function () {
    var c = controller(); //here save controller object

    if (locals.$scope && locals.$scope.init) {
        console.log('init'); 
        locals.$scope.init();
    }

    return c;//return controller;
}, controller);

Sample:

var myApp = angular.module('myApp',[]);

myApp.decorator('$controller', ['$delegate', function ($delegate) {
    return function (constructor, locals) {
        var controller = $delegate.apply(null, arguments);

        return angular.extend(function () {

          var c = controller();
          
        // Never got hear
        if (locals.$scope && locals.$scope.init) {
            console.log('init');
            locals.$scope.init();
        }
          
          return c;
        }, controller);
    };
}]);

myApp.controller('ctrl1', function($scope) {
	$scope.init = function() {
  	$scope.name = 'Superhero';
  };
  
});

myApp.controller('ctrl2', function($scope) {
	$scope.init = function() {
  	$scope.name = 'Mario';
  };
  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="ctrl1">
    Hello, {{name}}!
  </div>
  <div ng-controller="ctrl2">
    My name is, {{name}}!
  </div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Works like charm, Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.