AngularJs code to navigate to another page on a button click event. I have catch the button click event but cannot navigate to another page. The following is my code: HTML file:
<!DOCTYPE html>
<html>
<head>
<script src="js/angular.min.js"></script>
<script src="js/app/home.js"></script>
</head>
<body ng-app="myapp">
<div ng-controller="TestCtrl">
<button ng-click="clicked()">Click me!</button>
</body>
</html>
And the javascript file is:
var VLogin = angular.module('myapp',[]);
VLogin.controller('TestCtrl', ['$scope',function($scope) {
$scope.clicked = function(){
$location.path('/test.html');
}
}]);
Try like as shown below...
In html,
<button ng-click="clicked()">Click</button>
In JS,
$scope.clicked = function(){
window.location = "#/test.html";
}
I hope it helps you...
You can use $location.path('/newPageUrl') to navigate to new page.
Read here for more information on $location.
Edit: The correct controller code would be (after adding $location as a dependency):-
VLogin.controller('TestCtrl', ['$scope', '$location',function($scope, $location) {
$scope.clicked = function(){
$location.path('/test.html');
}
}]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With