Ich bin neu in Angular JS und versuche, einen Controller in einem anderen Controller aufzurufen, aber unter Fehler zu kommen.
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)
Fehlt mir hier etwas?
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;
})
Ist dies der beste Weg, um Controller in einen anderen Controller einzufügen? Ich habe nur eine gemeinsame Methode, die ich über alle Controller Art von Utility-Methode teilen möchte, aber es ruft Service und Verwendung von Versprechen Objekt legt den Wert in Scope-Variable.
Danke, Refaktoriert nach Ihrem Vorschlag. –