【轉】AngularJs $location獲取url引數

eedc發表於2016-12-29
// 帶#號的url,看?號的url,見下面  
url = http://qiaole.sinaapp.com?#name=cccccc  
  
$location.absUrl();  
// http://qiaole.sinaapp.com?#name=cccccc  
  
$location.host();  
// qiaole.sinaapp.com  
  
$location.port();  
// 80  
  
$location.protocol();  
// http  
  
$location.url();  
// ?#name=cccccc  
  
// 獲取url引數  
$location.search().name;  
// or  
$location.search()['name'];  
  
// 注:如果是這樣的地址:http://qiaole.sinaapp.com?name=cccccc  
  
var searchApp = angular.module('searchApp', []);  
searchApp.config(['$locationProvider', function($locationProvider) {  
  $locationProvider.html5Mode(true);  
}]);  
searchApp.controller('MainCtrl', ['$scope', '$location', function($scope, $location) {  
  if ($location.search().keyword) {  
    $scope.keyword = $location.search().keyword;  
  }  
}]);  

 

相關文章