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

标签 javascript angularjs dependency-injection angularjs-scope angularjs-controller

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

相关文章:

javascript - 针对 globar var 的私有(private) var 当全局改变时不改变

javascript - CSS 变量和 Angular 5

javascript - Angular 1.5 组件与旧指令 - 链接函数在哪里?

wcf - Ninject 和 WCF 服务授权管理器

c# - structuremap怎么装饰?

c# - 如何通过依赖注入(inject)使用 RateLimiter 的 HttpClient DelegatingHandler?

javascript - 为什么程序员在 React 路由中使用组件属性?

javascript - 使用 XMLHttpRequest 下载二进制数据,无需 overrideMimeType

javascript - TypeScript 中像 methodName<string>() 这样的尖括号有什么用?

javascript - 来自数据库的正则表达式在 Angular Directive(指令) ng-pattern 中不起作用