0

I have a controller that updates my awards scope:

Controller 1

.controller("awardController", ['$scope', '$rootScope', 'Restangular', "$q", "$location", "TokenRestangular",
    function ($scope, $rootScope, Restangular, $q, $location, TokenRestangular) {


        $scope.updateAwardScope = function () {
            resource = TokenRestangular.all('award');
            resource.getList()
                .then(function (awards) {
                    $scope.awards = awards;

                })
        }

    }])

Controller 2

I have another controller 2 with a click event thats outside of this controllers scope. Is it possible for the controller below to call the $scope.updateAwardScope function from controller 1?

.controller('MainController', function ($rootScope, $scope) {

 $scope.updateAwardScopeClick = function () {
    // somehow call function from controller 1 

  }


});
5
  • Please use factory for that. Commented Jul 22, 2014 at 16:31
  • @micronyks thanks for the reply. I've used a factory for passing variables around, but how can I use one for calling a function? Commented Jul 22, 2014 at 16:32
  • Besides a factory you can also put the function inside a parent/$rootScope controller. All child controllers will be able to use it. Commented Jul 22, 2014 at 16:40
  • 1
    "I've used a factory for passing variables around" - In javascript, functions can be variables. var my_function = function() { } ; So just do it the same way. Commented Jul 22, 2014 at 16:40
  • @dave thats interesting, do you have to evaluate it somehow so it knows its a function? Commented Jul 22, 2014 at 16:43

2 Answers 2

1

I've found the use of the factory/service pattern to be a very effective way of reusing code in angular applications. For this particular case you could create an AwardFactory, inject it in your controllers and then call the update function. i.e

AwardFactory

myApp.factory('AwardFactory', ['TokenRestangular', function(TokenRestangular.all) {
  var factory = {
    awards: []
  };

  factory.update = function() {
    resource = TokenRestangular.all('award');

    resource.getList().then(function (awards) {
      factory.awards = awards;
    });

    return factory.awards; // You can skip the return if you'd like that
  };

  return factory;
}]);

YourController

.controller('MainController', function ($rootScope, $scope, AwardFactory) {

 $scope.updateAwardScopeClick = function () { 
   AwardFactory.update();
 }
});

Hope it helps!

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

Comments

1

You can use angular broadcast and receive

Controller1

    .controller("awardController", ['$scope', '$rootScope', 'Restangular', "$q", "$location", "TokenRestangular",
function ($scope, $rootScope, Restangular, $q, $location, TokenRestangular) {


    $scope.updateAwardScope = function () {
        resource = TokenRestangular.all('award');
        resource.getList()
            .then(function (awards) {
                $scope.awards = awards;
                $rootScope.broadcast("update.awards");

            })
    }

}])

Controller 2

    .controller('MainController', function ($rootScope, $scope) {

     $rootScope.$on('update.awards', function(){
        $scope.updateAwardScopeClick();
     });

       $scope.updateAwardScopeClick = function () {
         // somehow call function from controller 1 

         }
       });

Comments

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.