gpt4 book ai didi

javascript - 类型错误 : $controller is not a function + Controller inside controller

转载 作者:行者123 更新时间:2023-11-28 18:38:30 28 4
gpt4 key购买 nike

我是 Angular JS 的新手,尝试在另一个 Controller 中调用一个 Controller ,但出现以下错误。

ionic.bundle.js:21157 TypeError: $controller is not a function
at new <anonymous> (http://localhost:8080/itravel/www/js/controllers.js:6:2)
at invoke (http://localhost:8080/itravel/www/lib/ionic/js/ionic.bundle.js:13277:17)
at Object.instantiate (http://localhost:8080/itravel/www/lib/ionic/js/ionic.bundle.js:13285:27)
at http://localhost:8080/itravel/www/lib/ionic/js/ionic.bundle.js:17841:28
at self.appendViewElement (http://localhost:8080/itravel/www/lib/ionic/js/ionic.bundle.js:52255:24)
at Object.switcher.render (http://localhost:8080/itravel/www/lib/ionic/js/ionic.bundle.js:50449:41)
at Object.switcher.init (http://localhost:8080/itravel/www/lib/ionic/js/ionic.bundle.js:50369:20)
at self.render (http://localhost:8080/itravel/www/lib/ionic/js/ionic.bundle.js:52115:14)
at self.register (http://localhost:8080/itravel/www/lib/ionic/js/ionic.bundle.js:52073:10)

我在这里错过了什么吗?

angular.module('starter.controllers', []).controller('DashCtrl', function($scope) {

}).controller('TransportController',['$scope','$controller', function($scope, TrainService, $ionicModal, $window, $controller) {
console.log("TransportController");

$controller('ProfileController', {
$scope: $scope
})

//$scope.testFunction();

$scope.transports = [];//TrainService.all();
$ionicModal.fromTemplateUrl('templates/modal.html', {
scope: $scope
}).then(function(modal) {
if (angular.equals(typeof $window.localStorage['profile'],'undefined')) {
modal.show();
} else {
$scope.profile = JSON.parse($window.localStorage['profile']);
if (angular.equals($scope.profile.city,'undefined') || angular.equals($scope.profile.city,'')) {
modal.show();
}
}
$scope.modal = modal;
});

$scope.saveUserProfile = function() {
console.log("Saving User Profile");
console.log($scope.profile);
$window.localStorage['profile'] = JSON.stringify($scope.profile);
$scope.modal.hide();
}

}]).controller('ProfileController', function($scope, $window, $ionicModal, TrainService, $ionicHistory,$timeout) {

if (!angular.equals($window.localStorage['profile'],'undefined')) {
$scope.profile = JSON.parse($window.localStorage['profile']);
}

$scope.selectedCity = "MUMBAI";

var promise = TrainService.listCities();
promise.then(function(resp){
$scope.cities = resp.data;
})

这是将 Controller 插入另一个 Controller 的最佳方法吗?我只有一个通用方法,我想在所有 Controller 类型的实用程序方法之间共享它,但它调用服务并使用 Promise 对象设置范围变量中的值。

最佳答案

问题出在 TransportController 的依赖关系内联数组上,其中大多数依赖关系都没有注入(inject)到 DI 数组中,因此相关实例在 Controller 工厂函数内未定义。

.controller('TransportController',['$scope','$controller', function($scope, TrainService, $ionicModal, $window,

应该是

.controller('TransportController',['$scope','$controller', 'TrainService', '$ionicModal', '$window', 
function($scope, $controller, TrainService, $ionicModal, $window,

理想情况下,您不应将一个 Controller 插入另一个 Controller 。您可以使用 service/factory 在应用程序的所有组件之间共享数据。

关于javascript - 类型错误 : $controller is not a function + Controller inside controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36553829/

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