[英]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
我是angular js的新手,不确定我是否了解ng-click和$ location.path
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: 以及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. 我只是希望一旦单击注册按钮就能转到/ register路径。
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. 按照您的代码
register
方法在RegisterController
内部,因此在“登录”页面上不可用。 Move this method to your login page controller it should work fine. 将此方法移到您的登录页面控制器,它应该可以正常工作。
You need to use $window: 您需要使用$ window:
app.controller('RegisterController', function($scope, $window) {
$scope.register = function() {
$window.location = '#/register';
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.