如果您的意图是获取另一个组件的已实例化控制器,并且如果您遵循基于组件/指令的方法,您始终可以 require 来自遵循某个特定组件的另一个组件的控制器(组件的实例)层次结构。
例如:
//some container component that provides a wizard and transcludes the page components displayed in a wizard
myModule.component('wizardContainer', {
...,
controller : function WizardController() {
this.disableNext = function() {
//disable next step... some implementation to disable the next button hosted by the wizard
}
},
...
});
//some child component
myModule.component('onboardingStep', {
...,
controller : function OnboadingStepController(){
this.$onInit = function() {
//.... you can access this.container.disableNext() function
}
this.onChange = function(val) {
//..say some value has been changed and it is not valid i do not want wizard to enable next button so i call container's disable method i.e
if(notIsValid(val)){
this.container.disableNext();
}
}
},
...,
require : {
container: '^^wizardContainer' //Require a wizard component's controller which exist in its parent hierarchy.
},
...
});
现在上面这些组件的用法可能是这样的:
<wizard-container ....>
<!--some stuff-->
...
<!-- some where there is this page that displays initial step via child component -->
<on-boarding-step ...>
<!--- some stuff-->
</on-boarding-step>
...
<!--some stuff-->
</wizard-container>
您可以通过多种方式设置require。
(无前缀) - 在当前元素上找到所需的控制器。找不到就报错。
? - 尝试定位所需的控制器,如果找不到,则将 null 传递给链接 fn。
^ - 通过搜索元素及其父元素找到所需的控制器。找不到就报错。
^^ - 通过搜索元素的父元素找到所需的控制器。找不到就报错。
?^ - 尝试通过搜索元素及其父元素来定位所需的控制器,如果未找到,则将 null 传递给链接 fn。
?^^ - 尝试通过搜索元素的父元素来定位所需的控制器,如果未找到,则将 null 传递给链接 fn。
旧答案:
您需要注入$controller 服务以在另一个控制器中实例化一个控制器。但请注意,这可能会导致一些设计问题。您始终可以创建遵循单一职责的可重用服务,并根据需要将它们注入控制器中。
例子:
app.controller('TestCtrl2', ['$scope', '$controller', function ($scope, $controller) {
var testCtrl1ViewModel = $scope.$new(); //You need to supply a scope while instantiating.
//Provide the scope, you can also do $scope.$new(true) in order to create an isolated scope.
//In this case it is the child scope of this scope.
$controller('TestCtrl1',{$scope : testCtrl1ViewModel });
testCtrl1ViewModel.myMethod(); //And call the method on the newScope.
}]);
在任何情况下,您都不能调用 TestCtrl1.myMethod(),因为您已将方法附加到 $scope 而不是控制器实例上。
如果您要共享控制器,那么最好这样做:-
.controller('TestCtrl1', ['$log', function ($log) {
this.myMethod = function () {
$log.debug("TestCtrl1 - myMethod");
}
}]);
在消费时做:
.controller('TestCtrl2', ['$scope', '$controller', function ($scope, $controller) {
var testCtrl1ViewModel = $controller('TestCtrl1');
testCtrl1ViewModel.myMethod();
}]);
在第一种情况下,$scope 是您的视图模型,而在第二种情况下,它是控制器实例本身。