[英]Register function doesn't working AngularJS and Java
I'm trying create registration form without Java controllers, only controllers in Angular. 我正在尝试创建注册表单,而没有Java控制器,只有Angular中的控制器。 But I have a problem with this controller UserRegistration in Angular.
但是我在Angular中有这个控制器UserRegistration的问题。 In my opion idea is good but i don't know good Angular as Java.
在我看来,好的想法是好的,但是我不知道Java的Angular是好的。
Here is my code: 这是我的代码:
User contructor public User(String name, String email, String passwordHash) 用户构造器公共用户(字符串名称,字符串电子邮件,字符串passwordHash)
Register resource: 注册资源:
@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 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'}
}
}
);
});
and my register.html pastebin: link 和我的register.html pastebin: 链接
Big thanks! 太谢谢了!
You are using the $resource
syntax for declaring actions to obtained resources, like so: 您正在使用
$resource
语法声明对获得的资源的操作,如下所示:
return $resource('rest/register/:action', {},
{
authenticate: {
method: 'POST',
params: {'action' : 'registration'},
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}
}
);
(see https://docs.angularjs.org/api/ngResource/service/ $resource) (请参阅https://docs.angularjs.org/api/ngResource/service/ $ resource)
But when you use the resource, you are not invoking that action. 但是,当您使用资源时,您不会调用该操作。 You should do this:
你应该做这个:
$scope.register = function() {
this.UserRegister.authenticate({username: $scope.user.username, email: $scope.user.emailAddress, password: $scope.user.password})
this.router.navigate(['/login']);
}
You also need to inject UserRegister
service in your controller 您还需要在控制器中注入
UserRegister
服务
function UserController($scope, $http, UserRegister) {
this.UserRegister = UserRegister
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.