Skip to content

Instantly share code, notes, and snippets.

@jorgeas80
Forked from siberex/ng.location.test.html
Created November 11, 2016 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgeas80/e4df00a62c5a27919714906d280df7f9 to your computer and use it in GitHub Desktop.
Save jorgeas80/e4df00a62c5a27919714906d280df7f9 to your computer and use it in GitHub Desktop.
AngularJS location provider example
<!doctype html>
<html lang="en"
xmlns:ng="http://angularjs.org" id="ng-app" ng-app="awesomeApp"
ng-strict-di="">
<head>
<title>Location test</title>
<base href="/ng.location.test.html/">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
<script>
"use strict";
(function(angular, undefined) {
var AwesomeAppName = angular.module('awesomeApp', []);
AwesomeAppName.config(['$locationProvider', function($locationProvider) {
$locationProvider.html5Mode({
enabled: true
// ,requireBase: false
});
}]);
AwesomeAppName.controller(
'CtrlDoAwesomeness',
['$rootScope', '$scope', '$location', '$http', function ($rootScope, $scope, $location, $http) {
// Adding watcher
$rootScope.$on("$locationChangeSuccess", function(event, next, current) {
// Do stuff when location changed
var url = decodeURIComponent( $location.url() );
console.log("url: " + url);
console.log("current location: " + current);
console.log("location changed to: " + next);
});
$scope.ohMyButton = function(event) {
console.log('Button clicked');
$location.path('/some/another/location');
};
}]
);
})(angular);
</script>
</head>
<body ng-controller="CtrlDoAwesomeness">
<div>
<h1>Location test</h1>
<br /><br />
<a href="some/location">Click The Link</a>
<br /><br />
<button type="button" ng-click="ohMyButton($event)">Click The Button</button>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment