返回到文章

采纳

编辑于

$location获取url参数 - AngularJs

$location获取url参数
AngularJS
笔记

$location比较简单,我们拿下面url举例:

带#号的url,和?号的url,如下

url = https://www.orchome.com?#name=cccccc

angularjs代码如下:

$location.absUrl();
// https://www.orchome.com?#name=cccccc

$location.host();
// www.orchome.com

$location.port();
// 80

$location.protocol();
// https

$location.url();
// ?#name=cccccc

// 获取url参数
$location.search().name;
// or
$location.search()['name'];

// 注:如果是这样的地址:https://www.orchome.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;
  }
}]);