本文介绍了AngularJS:从同一模块中的另一个控制器内部注入控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将控制器注入另一个属于同一模块的控制器?
Is possible to inject a controller into another controller that is part of the same module?
例如:
var app = angular.module('myAppModule', [])
.controller('controllerOne', ['$scope', function($scope){
$scope.helloWorld = function(){
return 'Hello World';
}
}])
.controller('controllerTwo', ['$scope', 'controllerOne', function($scope, controllerOne){
console.log(controllerOne.helloWorld());
}])
我一直在controllerOne上得到未知的提供者。我不知道它是如何可能的,因为它们在同一个模块中共存。任何帮助将不胜感激。
I keep getting unknown provider on controllerOne. I don't see how that's possible since they coexist in the same module. Any help would be much appreciated.
推荐答案
你需要使用 $ controller
依赖使用你可以将一个控制器注入另一个
You need to use $controller
dependency by using that you can inject one controller to another
.controller('controllerTwo', ['$scope', '$controller', function($scope, $controller){
$controller('controllerOne', {$scope: $scope})
//inside scope you the controllerOne scope will available
}]);
但是更喜欢 service / factory
来分享数据
这篇关于AngularJS:从同一模块中的另一个控制器内部注入控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!