简体   繁体   中英

not able to change the location path in angular JS

I have a login form and I am simply trying to change the path once the register button is selected. For some reason the login button works but the register button does not. When I click it nothing happens. Even if I put an alert inside the register controller nothing comes up which tells me that the controller function is not being executed. I am new to angular js and not sure if I understand ng-click and $location.path well

here is my code for the register button:

<button ng-click="register()" class="btn btn-default" id="lefty" >Register</button> 

and my code for the controller and the route provider in app.js:

$routeProvider.when('/register', {
        templateUrl: 'app/partials/register.html',
        controller: 'RegisterController'

      });



app.controller('RegisterController', function($scope, $location) {
        $scope.register = function() {
        $location.path('/register');
    }
});

I simply expect once register button is clicked to be able to go to the /register path.

I think you have the Register button on the login page. As per your code register method is inside RegisterController so it is not available on the Login page. Move this method to your login page controller it should work fine.

You need to use $window:

    app.controller('RegisterController', function($scope, $window) {
    $scope.register = function() {
    $window.location = '#/register';
}
});

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