AngularJs $location获取url参数

 http://blog.gaoqixhb.com/p/5677fb7c6a55cd573cb30db2

有时候遇到需要在url上给angular传参的情况,这种时候可以用$location.search()方法来获取

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

你可能感兴趣的:(AngularJs $location获取url参数)