0
當我點擊提交按鈕時,如何獲得提示錯誤消息,並且由於沒有輸入電子郵件/密碼的數據庫中沒有重定向到其他頁面。驗證提交按鈕(angularjs)
登錄in.html
<form class="contact" ng-submit="login()">
<div class="container">
<h3>Sign in</h3>
<div class="contact-form">
<div class="col-md-6 col-md-offset-3">
<input type="email" ng-model="user.email" placeholder="Email address" required autofocus>
<div>
<input type="password" ng-model="user.password" placeholder="Password" required autofocus>
</div>
<div class="clearfix"> </div>
<button type="submit">Submit</button>
</div>
</div>
</div>
</form>
auth.js(控制器)
angular
.module('app')
.controller('AuthLoginController', ['$scope', 'AuthService', '$state',
function($scope, AuthService, $state) {
$scope.user = {
email: '[email protected]',
password: 'example123'
};
$scope.login = function() {
AuthService.login($scope.user.email, $scope.user.password)
.then(function() {
$state.go('home');
});
};
}])
auth.js(服務)
angular
.module('app')
.factory('AuthService', ['Viewer', '$q', '$rootScope', function(User, $q,
$rootScope) {
function login(email, password) {
return User
.login({email: email, password: password})
.$promise
.then(function(response) {
$rootScope.currentUser = {
id: response.user.id,
tokenId: response.id,
email: email
};
});
}
return {
login: login,
logout: logout,
register: register
};
}]);