我正在尝试从控制器到另一个控制器调用函数几个小时。
我看到了类似的主题(我试过,但我当然太糟糕了): What's the correct way to communicate between controllers in AngularJS? Can one controller call another?
但没有像我正在寻找的具体例子。
控制器:
app.controller('Controller1', function($scope, $modal, $modalInstance, job, JobServices) {
$scope.job1 = function () {
JobServices.job1($scope.name)
.success(function(data, status, headers, config) {
// something
});
}
function doSomething(){
// How call test() from controller2 ?
}
}
app.controller('Controller2', function($scope){
function test(){
do a lot of things
}
}
最好和最简单的方法是什么?
非常感谢
编辑:
我已经尝试过使用类似的东西了:
app.controller('Controller1', ['$scope', '$rootScope', function($scope, $rootScope, $modal, $modalInstance, job, JobServices) {
但我有错误说job1未定义......
答案 0 :(得分:0)
你可以使用$ scope。$ emit和$ scope。$ on,有点像这样
app.controller('Controller1', ['$scope', '$rootScope' function($scope) {
function doSomething(){
$rootScope.$emit("callController2", {});
}
}
app.controller('Controller2', ['$scope', '$rootScope' function($scope) {
$rootScope.$on("callController2", function(){
this.test();
});
function test(){
do a lot of things
}
}