AngularJS的$location使用方法詳解
一、配置config
app.config([ '$locationProvider', function($locationProvider) { $locationProvider.html5Mode({ //設置為html5Mode(模式),當為false時為Hashbang模式 enabled : true, //是否需要加入base標簽,這里設置為false,設置為true時,需在html的head配置<base href="" />標簽 requireBase : false }); } ]); 注意:config里面的配置非常重要,不配置獲取不到url里面的參數
二、基本用法
url:http://127.0.0.1:7001/liuxu/pages/main.html?name=5 1.獲取絕對路徑 $location.absUrl(); //url:http://127.0.0.1:7001/liuxu/pages/main.html?name=5 2.獲取主機 $location.host(); http://127.0.0.1 3.獲取端口號 $location.port(); //7001 4.獲取文本傳輸協議 $location.protocol(); http 5. 獲取url參數 $location.search().name或者$location.search()['name'] //5 6.獲取url $location.url() //:/liuxu/pages/main.html?name=5
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答
圖片精選