gpt4 book ai didi

angularjs - 如何在 AngularJS 中将一个 Controller 注入(inject)另一个 Controller

转载 作者:行者123 更新时间:2023-12-03 04:54:15 25 4
gpt4 key购买 nike

我是 Angular 新手,正在尝试弄清楚如何做事......

使用 AngularJS,如何注入(inject)一个 Controller 以在另一个 Controller 中使用?

我有以下代码片段:

var app = angular.module("testApp", ['']);

app.controller('TestCtrl1', ['$scope', function ($scope) {
$scope.myMethod = function () {
console.log("TestCtrl1 - myMethod");
}
}]);

app.controller('TestCtrl2', ['$scope', 'TestCtrl1', function ($scope, TestCtrl1) {
TestCtrl1.myMethod();
}]);

当我执行此操作时,出现错误:

Error: [$injector:unpr] Unknown provider: TestCtrl1Provider <- TestCtrl1
http://errors.angularjs.org/1.2.21/$injector/unpr?p0=TestCtrl1Provider%20%3C-%20TestCtrl1

我是否应该尝试在另一个 Controller 中使用一个 Controller ,或者我应该将其作为一项服务?

最佳答案

如果您的目的是获取另一个组件的已实例化的 Controller ,并且如果您遵循基于组件/指令的方法,那么您始终可以需要来自另一个组件的 Controller (组件的实例)遵循一定层次结构的组件。

例如:

//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

(no prefix) - Locate the required controller on the current element. Throw an error if not found.

? - Attempt to locate the required controller or pass null to the link fn if not found.

^ - Locate the required controller by searching the element and its parents. Throw an error if not found.

^^ - Locate the required controller by searching the element's parents. Throw an error if not found.

?^ - Attempt to locate the required controller by searching the element and its parents or pass null to the link fn if not found.

?^^ - Attempt to locate the required controller by searching the element's parents, or pass null to the link fn if not found.

<小时/><小时/>

旧答案:

您需要注入(inject)$controller在另一个 Controller 中实例化一个 Controller 的服务。但请注意,这可能会导致一些设计问题。您始终可以创建遵循单一职责的可重用服务,并根据需要将它们注入(inject) 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 实例上。

如果您共享 Controller ,那么最好这样做:-

.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 实际上是您的 View 模型,在第二种情况下,它是 Controller 实例本身。

关于angularjs - 如何在 AngularJS 中将一个 Controller 注入(inject)另一个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25417162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com