gpt4 book ai didi

javascript - 在 AngularJS 中将一个 Controller 注入(inject)另一个 Controller 时出错

转载 作者:行者123 更新时间:2023-12-02 16:44:51 25 4
gpt4 key购买 nike

我的代码是这样的

Controller.JS
angular.module('RateRequestApp.controllers', []).controller('ReadOnlyController', '$controller', function($scope, rateRequestService,$controller) {

$controller('ModalDemoCtrl',{$scope : modalDCtrl });
$scope.rateData = [];

rateRequestService.getData().success(function(response) {
$scope.rateData = response;
}).error(function (data, status, headers, config) {

modalDCtrl.openModal();
});
});
angular.module('RateRequestApp.controllers').controller('ModalDemoCtrl', function ($scope, $modal, $log) {

this.openModal = function (size) {

var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size,

});
};
});
App.JS
angular.module('RateRequestApp', [
'RateRequestApp.services',
'RateRequestApp.controllers',
'ui.bootstrap'
]);

对我来说一切看起来都很好,但这会引发错误

Error: [ng:areq] Argument 'ReadOnlyController' is not a function, got string

有人可以指出我做错了什么吗?

最佳答案

您错误地使用了依赖声明。它需要被包装在一个数组中。而不是:

.controller('ReadOnlyController',  '$controller', function($scope, rateRequestService,$controller) {
// your stuff
});

尝试:

.controller('ReadOnlyController',  ['$controller', function($scope, rateRequestService,$controller) {
// your stuff
}]);

即使这样也是不正确的,因为依赖项的名称和参数需要匹配。尝试:

.controller('ReadOnlyController',  ['$scope','rateRequestService','$controller', function($scope, rateRequestService,$controller) {
// your stuff
}]);

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

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