gpt4 book ai didi

javascript - 不同文件中的 Angular JS 服务和 Controller 导致错误

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

在我的 angularjs 项目中,我有以下文件:

/index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>

<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">

<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

<!-- Custom Libraries -->
<script src="lib/Shake.js"></script>

<!-- your app's js -->

<script src="js/services/CloudDatabases.js"></script>
<script src="js/controllers.js"></script>
<script src="js/app.js"></script>

</head>

<body ng-app="starter">
<ion-nav-view></ion-nav-view>
</body>
</html>

/js/apps.js

// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])

.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.StatusBar) {
StatusBar.styleDefault();
}

// Register stopping and starting analytics on app open and close
document.addEventListener("pause", window.analytics.Stop(), false);
document.addEventListener("resume", window.analytics.Start(), false);

// Exit the application if you go offline
document.addEventListener("offline", function(){}, false);
});
})

.config(function($stateProvider, $urlRouterProvider) {
$stateProvider

.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/menu.html",
controller: 'AppCtrl'
})

....
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/login');
});

/js/services/CloudDatabases.js

angular.module('starter.services')
.service('CloudDatabases', ['$http', function ($http) {

var urlBase = '/api/customers';

this.getDatabases = function () {
console.log('CloudDatabases.getDatabases();');
return 'test getDatabasesResponse';
};
}])

/js/controllers.js

angular.module('starter.controllers', ['CloudDatabases'])

// Login controller
.controller('LoginCtrl', function($scope, $ionicLoading, $http, $ionicPopup, $rootScope, $state, $ionicViewService, CloudDatabases) {
CloudDatabases.getDatabases();

// Form data for the login modal
$scope.loginData = {};

// Try loading the loing data from storage if the user has already logged in
$scope.loginData.username = window.localStorage['username'] || '';
$scope.loginData.password = window.localStorage['password'] || '';
$scope.loginData.uk = window.localStorage['uk'] || false;

// Perform the login action when the user submits the login form
$scope.doLogin = function() {
// Show the loading overlay so the user knows we are busy
$ionicLoading.show({template: 'Loading...'});

// Save the login data to local storage so if the user closes the app they
// don't have to re-enter it
window.localStorage['username'] = $scope.loginData.username;
window.localStorage['password'] = $scope.loginData.password;
window.localStorage['uk'] = $scope.loginData.uk;

// Build login JSON from form
var login_json = JSON.stringify({auth: {passwordCredentials: {username: $scope.loginData.username, password: $scope.loginData.password}}});

// POST the actual authentication request
$http({
method: 'POST',
url: 'https://identity.api.rackspacecloud.com/v2.0/tokens',
data: login_json,
headers: {'Content-Type': 'application/json'}
}).then(function(response) {

// Save the auth token and tenant id for later use
$rootScope.userData = [];
$rootScope.userData.Token = response.data.access.token.id;
$rootScope.userData.Tenant = response.data.access.token.tenant.id;
$rootScope.userData.RawServices = response.data.access.serviceCatalog;

// Use viewservice to hide back button on next page and remove login from nav stack
$ionicViewService.nextViewOptions({
disableBack: true
});

// Track successful logins
window.analytics.trackFeature("Login.Success");

$ionicLoading.hide();

// Navigate to Servers page
$state.go('app.servers');
},
function(response) {
// Track failed logins
window.analytics.trackFeature("Login.Failure");

$ionicLoading.hide();
});
};
})
....

但这会抛出一个错误,说它不能被注入(inject)。

任何人都可以帮助解释为什么会这样吗?它说 starter.services 没有定义

最佳答案

错误是因为您的服务定义。您正在使用没有 dependency array [] 作为第二个参数的服务定义。这告诉 Angular 将其视为模块 starter.services 的 getter 方法,而不是定义模块。使用下面的 starter.services 代码来解决这个问题。

angular.module('starter.services', [])
.service('CloudDatabases', ['$http', function ($http) {

var urlBase = '/api/customers';

this.getDatabases = function () {
console.log('CloudDatabases.getDatabases();');
return 'test getDatabasesResponse';
};
}])

关于javascript - 不同文件中的 Angular JS 服务和 Controller 导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25292613/

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