Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs code to navigate to another page in buttonclick

Tags:

angularjs

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');
    }

}]);
like image 676
Bibin Jose Avatar asked Dec 05 '25 13:12

Bibin Jose


2 Answers

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...

like image 160
SDK Avatar answered Dec 07 '25 03:12

SDK


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');
}

}]);

like image 41
Abhishek Jain Avatar answered Dec 07 '25 03:12

Abhishek Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!