AngularJS Intercept and extend controller $scope

后端 未结 1 405
天涯浪人
天涯浪人 2021-02-06 00:00

There is a lot of reusable functionality that I have defined in my application that EVERY controller uses with the $scope variable. Instead of me having to create a shared servi

相关标签:
1条回答
  • 2021-02-06 00:31

    Instead of .config try .run, this will do exactly what you want.

    angular.module('App', []).run(['$rootScope', function($rootScope) {
      $rootScope.foo = function() {
         alert("WIN!");
      };
    }]);
    
    angular.module('App').controller('HomeCtr', ['$scope', function($scope) {
      $scope.foo(); #will call the alert
    }]);
    

    NOTE I have only used module.controller because I like it, var HomeCtrl = function($scope) { will have the same effect.

    0 讨论(0)
提交回复
热议问题