AngularJs Change Url


AngularJs Change Url – Sometimes we need to change the url in angularjs. You can use $location.url to redirect from one page to another page. Here in this tutorial we are going to explain how you can redirect in angularjs. You can use our online editor to edit and run the code online.


AngularJs Change Url

You can change the current using the following syntax-

AngularJs Change Url Example:

 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope,$location) {
$scope.changeUrl = function(userId) {
	 $location.url('/User/'+userId);
	 // will change the current url to www.example.com/User/98
};
});
</script> 

$location.url(‘/User/’+userId); will set the current url to the new url example – www.example.com/User/98. You can call the function – changeUrl(userId) on button click.

Note : Make Sure the service $location is imported before using it.

Advertisements

Add Comment

📖 Read More