gpt4 book ai didi

angularjs - 在Controller中访问Angularjs中的URL参数

转载 作者:行者123 更新时间:2023-12-02 22:21:27 27 4
gpt4 key购买 nike

如何通过 angularJS Controller 访问 URL 参数。例如:我想读取 Controller 中的参数1和参数2,以便我可以在 Controller 中采取适当的操作。 http://localhost:8080/MyApplication?Parameter1=testresponse&parameter2=1234

最佳答案

花了一些时间后,我发现 $location 就是我正在寻找的那个。下面是示例代码片段。 >>AngularJS v1.4.8

ma​​in.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:参数是:..测试

警报 2:“在浏览器中输入的网址是:” + http://localhost:8180/Application/#param1=test

关于angularjs - 在Controller中访问Angularjs中的URL参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36573129/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com