我使用loopback作为后端,postgresql使用数据库。所以我尝试使用lb-service / js。
如果我试图在login.js中加入lbServices(angular.module(' myApp',[' ngRoute',' lbServices'])等等)意味着得到错误。
错误详情:
未捕获错误:[$ injector:modulerr]无法实例化模块 myApp由于:错误:[$ injector:modulerr]无法实例化模块 ngRoute由于:错误:[$ injector:nomod] Module' ngRoute'不是 可以!您要么错误拼写了模块名称,要么忘记加载它。 如果注册模块,请确保将依赖项指定为 第二个论点。
的index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Myapps</title>
<!-- Bootstrap -->
<link href="vendor/bootstrap/dist/css/bootstrap.css" rel="stylesheet">
<script src="vendor/theme_files/js/jquery.min.js"></script>
<script src="vendor/angular/angular.js"></script>
<script src="vendor/angular-resource/angular-resource.js"></script>
<script src="vendor/angular-ui-router/release/angular-ui-router.js"></script>
<script src="js/services/lb-services.js"></script>
</head>
<body ng-app="myApp" class="nav-md " style="background:#F7F7F7;">
<div class="container body" ui-view></div>
<script src="vendor/bootstrap/dist/js/bootstrap.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers/loginController.js"></script>
<script src="js/services/login.js"></script>
</body>
</html>
的login.html
<div id="wrapper">
<div id="login" class="animate form">
<section class="login_content">
<form ng-submit="login()">
<br>
<h1>Login</h1>
<div>
<input type="text" class="form-control" ng-model="loginForm.username" placeholder="Username" required="" />
</div>
<div>
<input type="password" class="form-control" ng-model="loginForm.password" placeholder="Password" required="" />
</div>
<div>
<button type="submit" class="btn btn-default submi" ng-disabled="disabled">
Log in
</button>
<a class="reset_pass" href="#">Lost your password?</a>
</div>
<div ng-show="error" class="alert alert-danger">
{{errorMessage}}
</div>
</form>
</section>
<!-- content -->
</div>
</div>
app.js
angular.module('myApp', ['lbServices', 'ui.router']).config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider.state('todo', {
url : '',
templateUrl : 'views/login.html',
controller : 'loginController'
}).state('login', {
url : '/login',
templateUrl : 'views/login.html',
controller : 'loginController'
}).state('profile', {
url : '/profile',
templateUrl : 'views/profile.html',
controller : 'loginController'
});
$urlRouterProvider.otherwise('login');
}]);
logincontroller.js
angular.module('myApp').controller('loginController', ['$scope', '$location', 'AuthService',
function($scope, $location, AuthService) {
$scope.login = function() {
// initial values
$scope.error = false;
$scope.disabled = true;
// call login from service
AuthService.login($scope.loginForm.username, $scope.loginForm.password)
// handle success
.then(function() {
$location.path('/index');
$scope.disabled = false;
$scope.loginForm = {};
})
// handle error
.catch(function() {
$scope.error = true;
$scope.errorMessage = "Invalid username and/or password";
$scope.disabled = false;
$scope.loginForm = {};
});
};
}]);
服务/ login.js
angular.module('myApp',['ngRoute' ,'lbServices']).factory('AuthService', ['$q', '$timeout', '$http',
function($q, $timeout, $http) {
// create user variable
var user = null;
// return available functions for use in controllers
return ( {
isLoggedIn : isLoggedIn,
getUserStatus : getUserStatus,
login : login,
logout : logout,
register : register
});
function isLoggedIn() {
if (user) {
return true;
} else {
return false;
}
}
function getUserStatus() {
return user;
}
function login(username, password) {
// create a new instance of deferred
var deferred = $q.defer();
// send a post request to the server
$http.post('/api/login', {
username : username,
password : password
})
// handle success
.success(function(data, status) {
if (data.status === 200 && data.status) {
user = true;
deferred.resolve();
} else {
user = false;
deferred.reject();
}
})
// handle error
.error(function(data) {
user = false;
deferred.reject();
});
// return promise object
return deferred.promise;
}
}]);
答案 0 :(得分:0)
将login.js编辑为
angular.module('myApp',['ui.router' ,'lbServices'])