在Controller中的Angularjs中访问URL参数 [英] Access URL parameters in Angularjs in the Controller
问题描述
如何通过angularJS控制器访问URL参数.例如:我想在控制器中读取parameter1和parameter2,以便可以在控制器中采取适当的措施. http://localhost:8080/MyApplication?Parameter1 = testresponse& parameter2 = 1234
How to access the URL parameters through the angularJS controller. For example: i would like to read parameter1 and parameter2 in a controller so that i can take appropriate action in my controller. http://localhost:8080/MyApplication?Parameter1=testresponse¶meter2=1234
推荐答案
花了一段时间后,我发现$ location是我要搜索的位置.以下是示例代码段. >> AngularJS v1.4.8
After spending some time, i figured that $location is the one I was searching for. Below is sample code snippet. >>AngularJS v1.4.8
main.js
var mainApp = angular.module("app",['ui.bootstrap','ngModal']);
mainApp.config(['$locationProvider',function($locationProvider){
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
var mainController = function($scope,$window,$rootScope, $cookies,$cookieStore,$location){
var searchObject = $location.search();
window.alert("parameter is :.."+searchObject.param1);
window.alert("Url typed in the browser is: "+$location.absUrl());
}
mainApp.controller("MainController",["$scope","$window","$rootScope",'$location',mainController]);
在浏览器中输入以下URL: http://localhost:8180/Application/#param1 = test
输出:
警报1:参数为:..测试
alert 1: parameter is :.. test
警报2:在浏览器中键入的网址是:" + http://localhost:8180/应用程序/#param1 = test
alert 2: "Url typed in the browser is: " +http://localhost:8180/Application/#param1=test
这篇关于在Controller中的Angularjs中访问URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!