单击表单上的“提交”按钮时发生错误。
LoginController和controllerAs在路由配置中定义
Angular版本:AngularJS v1.3.11
dataService.js文件
'use strict';
app.factory('dataService',['$http',DataService]);
function DataService($http) {
var urlBase='http://washerycloud.cloudapp.net/v1/user/';
var service = {};
service.Login =Login;
service.Register= Register;
service.Store = StoreID;
service.Update = Update;
service.Delete = Delete;
return service;
/**Login function*/
function Login(user) {
return $http.get(urlBase + 'login',{params: {name: user.name, password: user.password}}).
then(handleSuccess, handleError('Error login'));
}
/**Register function*/
function Register(user) {
return $http.get(urlBase + 'create',
{params: { name: user.name, surname: user.surname, telephone: user.telephone,
email: user.email, username: user.username, password: user.password }})
.then(handleSuccess, handleError('Error register user'));
}
// private functions
function handleSuccess(data) {
return data;
}
function handleError(error) {
return function () {
return { success: false, message: error };
};
}
}
LoginController.js文件
'use strict';
// login controller
app.controller('LoginController',
[ '$state','$window','dataService',
Register]);
function Register( $state, dataService, $window) {
var vm=this;
vm.user = {};
vm.authError = null;
vm.login = function() {
vm.authError = null;
// Try to login
dataService.Login(vm.user)
.then(function(response) {
if ( !response.data.user ) {
vm.authError = 'Email or Password not right';
$window.alert(response);
}else{
// $state.go('app.dashboard-v1');
$window.alert(response);
}
}, function(x) {
vm.authError = 'Server Error';
});
};
};
--------------------------------------------------------------------
的login.html
<form name="form" class="form-validation">
<div class="text-danger wrapper text-center" ng-show="authError">
{{authError}}
</div>
<div class="list-group list-group-sm">
<div class="list-group-item">
<input placeholder="Name" class="form-control no-border" ng-model="vm.user.name" required>
</div>
<div class="list-group-item">
<input type="password" placeholder="Password" class="form-control no-border" ng-model="vm.user.password" required>
</div>
</div>
<button type="submit" class="btn btn-lg btn-primary btn-block" ng-click="login()" ng-disabled='form.$invalid'>Log in</button>
<div class="text-center m-t m-b"><a ui-sref="access.forgotpwd">Password dimenticata?</a></div>
<div class="line line-dashed"></div>
<p class="text-center"><small>Non hai un account?</small></p>
<a ui-sref="access.signup" class="btn btn-lg btn-default btn-block">Registrati !</a>
</form>
----------------------------------------------------------------------------
错误消息
TypeError: dataService.Login is not a function
at Register.vm.login (login.js:17)
at $parseFunctionCall (angular.js:12336)
at angular-touch.js:472
at Scope.$get.Scope.$eval (angular.js:14388)
at Scope.$get.Scope.$apply (angular.js:14487)
at HTMLButtonElement.<anonymous> (angular-touch.js:471)
at HTMLButtonElement.n.event.dispatch (jquery.js:4430)
at HTMLButtonElement.n.event.add.r.handle (jquery.js:4116)
答案 0 :(得分:5)
在数组注释和控制器函数的参数列表中,注入服务的顺序必须相同。
更改您的控制器声明,以便注入服务的顺序排列......
app.controller('LoginController', [
'$state','$window','dataService',Register]);
function Register( $state, $window, dataService) {
...