【转】AngularJs $location获取url参数

// 带#号的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;  
  }  
}]);  

 

posted @ 2016-12-29 18:46  eedc  阅读(1726)  评论(0编辑  收藏  举报