调用的控制器的功能在AngularJS另一个模块 [英] Call a function in a controller in another module in AngularJS

查看:310
本文介绍了调用的控制器的功能在AngularJS另一个模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法调用另一个控制器是在另一个模块中的功能。我不知道这是可能的,但是这是我现在有:

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));
            }
        }
    );

这是给我的错误:

Unknown provider: recipeCtrlProvider <- recipeCtrl <- SearchCtrl

任何人都知道如何完成我所试图做的?

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

推荐答案

控制器不能注射,所以它简单不起作用。您需要创建与功能的服务,并注入该服务。

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.

最后一个资源应该用rootScope并观看了价值,但它不建议这样做。

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

这篇关于调用的控制器的功能在AngularJS另一个模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆