1. 程式人生 > >AngularJs中url參數的獲取

AngularJs中url參數的獲取

.config [] 方法 earch onf provide mod angular com

前言:

  angular獲取通過鏈接形式訪問的頁面,要獲取url中的參數,就不能通過路由的方式傳遞獲取了,使用原生js或者jquery,又顯得比較麻煩,好在angular已經封裝了獲取url參數的方法,而且只需要一行代碼搞定,簡單粗暴,完勝傳統的url截取匹配字符串的方式。

var para=$location.$$search["para"]

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; } }]);

AngularJs中url參數的獲取