1

I am having trouble calling a function in another controller that is in another module. I don't know if this is possible, but this is what I have now:

angular.module('search.controllers', ['socket.services', 'recipe.controllers'])
    .controller('SearchCtrl', ['$scope', '$state', 'socketService', 'recipeCtrl', 
        function($scope, $state, socketService, recipeCtrl) {
            recipeCtrl.setRecipe({});
        }
]);

angular.module('recipe.controllers', [])
    .controller('recipeCtrl', 
        function($scope) {
            this.onRecipeSelect = function(recipe) {
                console.log(JSON.stringify(recipe));
            }
        }
    );

This is giving me the error:

Unknown provider: recipeCtrlProvider <- recipeCtrl <- SearchCtrl

Anyone know how to accomplish what I am trying to do?

1
  • You can't inject controller, they are not services. So no, it's not possible. You need actual service for this. Commented Nov 23, 2015 at 22:39

1 Answer 1

3

Controllers can't be injected, so it simple does not work. You need to create a service with your function and inject this service.

If you need to exchange data between controllers the best option is to use events but your controllers need to be nested in order to communicate via scopes.

One last resource should use rootScope and watch for a value, but its not recommended to do it.

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

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.