AngularJs的内置服务$window,$document,$location
宣煜
2023-12-01
<!DOCTYPE html>
<html ng-app="myapp">
<head>
<meta charset="UTF-8">
<title>各种service</title>
<script src="../angular-1.5.5/angular.min.js"></script>
<script>
var my=angular.module("myapp",[]);
my.controller("myCtrl",function($scope,$window,$document,$location){
$scope.wHeight=$window.innerHeight;
$scope.wWidth=$window.innerWidth;
//-------------------------------------------设置title
//$document[0].title="呵呵";
$scope.title=$document[0].title;
$scope.protocol=$location.protocol();//--------URL协议部分
$scope.hostname=$location.host();//------------URL主机部分
$scope.port=$location.port();//----------------端口号
$location.path("aaa");//-----------------------设置URL中的 hash部分
$scope.path=$location.path();
$scope.absUrl=$location.absUrl();//-------------获得URL
})
</script>
</head>
<body ng-controller="myCtrl">
<p>屏幕高度{{wHeight}}</p>
<p>屏幕宽度{{wWidth}}</p>
<p>页面title{{title}}</p>
<p>获取url协议{{protocol}}</p>
<p>获取url主机{{hostname}}</p>
<p>获取端口号{{port}}</p>
<p>获取hash部分{{path}}</p>
<p>获取访问地址{{absUrl}}</p>
</body>
</html>