Last active
November 11, 2016 09:40
-
-
Save siberex/405ae3acf97b11b1cb44 to your computer and use it in GitHub Desktop.
AngularJS location provider example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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
Thanks!