[英]Register function doesn't working AngularJS and Java
我正在嘗試創建注冊表單,而沒有Java控制器,只有Angular中的控制器。 但是我在Angular中有這個控制器UserRegistration的問題。 在我看來,好的想法是好的,但是我不知道Java的Angular是好的。
這是我的代碼:
用戶構造器公共用戶(字符串名稱,字符串電子郵件,字符串passwordHash)
注冊資源:
@Component
@Path("/register")
public class RegisterResource {
@Autowired
private UserDao userDao;
@Path("registration")
@POST
@Produces(MediaType.APPLICATION_JSON)
public User user(User user)
{
user(user).addRole(Role.USER);
return this.userDao.save(user);
}}
app.js
angular.module('exampleApp', ['ngRoute', 'ngCookies', 'exampleApp.services'])
.config(
[ '$routeProvider', '$locationProvider', '$httpProvider', function($routeProvider, $locationProvider, $httpProvider) {
$routeProvider.when('/register', {
templateUrl: 'partials/register.html',
controller: UserController
});
function UserController($scope, $http) {
$scope.user = {};
$scope.register = function() {
this.UserRegister.user({username: $scope.user.username, email: $scope.user.emailAddress, password: $scope.user.password})
this.router.navigate(['/login']);
}
}
var services = angular.module('exampleApp.services', ['ngResource']);
services.factory('UserRegister', function($resource) {
return $resource('rest/register/:action', {},
{
authenticate: {
method: 'POST',
params: {'action' : 'registration'},
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}
}
);
});
和我的register.html pastebin: 鏈接
太謝謝了!
您正在使用$resource
語法聲明對獲得的資源的操作,如下所示:
return $resource('rest/register/:action', {},
{
authenticate: {
method: 'POST',
params: {'action' : 'registration'},
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}
}
);
(請參閱https://docs.angularjs.org/api/ngResource/service/ $ resource)
但是,當您使用資源時,您不會調用該操作。 你應該做這個:
$scope.register = function() {
this.UserRegister.authenticate({username: $scope.user.username, email: $scope.user.emailAddress, password: $scope.user.password})
this.router.navigate(['/login']);
}
您還需要在控制器中注入UserRegister
服務
function UserController($scope, $http, UserRegister) {
this.UserRegister = UserRegister
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.