AngularJS $location address service

Service: An object or function that provides specific functionality to the outside world.


demo.html:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>AngularJS</title>
	<script src="angular.min.js"></script> <!-- Introduce AngularJS framework-->
</head>
<body ng-app="App">
	<dl ng-controller="DemoController">
		<dt>{{title}}</dt>
		<dd>absUrl:{{absUrl}}</dd>
		<dd>url:{{url}}</dd>
		<dd>host:{{host}}</dd>
		<dd>search:{{search}}</dd>
		<dd>hash:{{hash}}</dd>
		<dd>protocol:{{protocol}}</dd>
		<dd>port:{{port}}</dd>
	</dl>
	<script>
		
		var App = angular.module('App',[]);

		// $location encapsulates the service for the address
		App.controller("DemoController",['$scope','$location',function($scope,$location) {
			$scope.title = "Learn about $location service";
			$scope.absUrl = $location.absUrl(); // Current address. Must have server to take effect
			$scope.url = $location.url(); //The content after the anchor (#) in the address (equivalent to the hash in the location object in the native DOM)
			$scope.host = $location.host(); // hostname

			$scope.search = $location.search(); //Query parameters after the anchor in the address (different from the search of the native location object)
			$scope.hash = $location.hash(); //The content after the anchor after the anchor. (content after two anchors) (different from native)
			$scope.protocol = $location.protocol();  // 协议 http
			$scope.port = $location.port(); // port

		}]);

		// location object in native DOM
		for(key in location){
			console.log(key + "----" + location[key]); //hash is the content after the anchor (#)
		}
		
	</script>
</body>
</html>


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325948420&siteId=291194637