简体   繁体   中英

Register function doesn't working AngularJS and Java

I'm trying create registration form without Java controllers, only controllers in Angular. But I have a problem with this controller UserRegistration in Angular. In my opion idea is good but i don't know good Angular as Java.

Here is my code:

User contructor public User(String name, String email, String 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

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

Big thanks!

You are using the $resource syntax for declaring actions to obtained resources, like so:

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)

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

function UserController($scope, $http, UserRegister) {
    this.UserRegister = UserRegister

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM