简体   繁体   English

访问URL参数 - PHP和Angular JS

[英]Accessing URL parameters - PHP & Angular JS

So my website is PHP for the backend and AngularJS for the frontend. 所以我的网站是用于后端的PHP和用于前端的AngularJS。 Weird that I'm finding that I have to use PHP on the frontend to achieve some things like getting URL paramters. 奇怪的是,我发现我必须在前端使用PHP来实现一些诸如获取URL参数之类的东西。 Explanation below; 说明如下;

Given the following URLs for example 例如,给出以下URL

http://www.test.co.uk/search-menu/1/cinamon-soho
http://www.test.co.uk/search-restaurant?location=asokoro&day=today&time=1100

My Angular code in the same page requires the parameters location, day etc. Right now I'm having to use the line below to pass them to angular 我在同一页面中的Angular代码需要参数location,day等。现在我必须使用下面的行将它们传递给angular

$scope.l = <?php echo json_encode($_GET['location']); ?>;

My questions are; 我的问题是;

  1. Is there a way to access these variables using just Angular so I can take PHP out of the equation? 有没有办法只使用Angular访问这些变量,所以我可以把PHP从等式中取出来?
  2. If question 1 is possible, how can I do the same if I then decide to move my Angular code away from that page to a dedicated .js page that I reference using 如果问题1是可能的,如果我决定将我的Angular代码从该页面移到我引用的专用.js页面,我怎么能这样做呢?

<script src="http://www.test.co.uk/js/main.js"></script>

FYI FYI

The website was not built from the ground up using AngularJS. 该网站不是使用AngularJS从头开始构建的。 Angular was later introduced to the frontend heavy lifting PHP was doing so the website is not a SPA . Angular后来被引入到前端繁重的PHP正在这样做的网站不是SPA There's no angular routing in place. 没有角度布线。

Thanks. 谢谢。

I got this code that works fine : 我得到这个代码工作正常:

function $_GET(param) {
    var vars = {};
    window.location.href.replace( 
        /[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
        function( m, key, value ) { // callback
            vars[key] = value !== undefined ? value : '';
        }
    );

    if ( param ) {
        return vars[param] ? vars[param] : null;    
    }
    return vars;
}

(source : http://www.creativejuiz.fr/blog/javascript/recuperer-parametres-get-url-javascript ) (来源: http//www.creativejuiz.fr/blog/javascript/recuperer-parametres-get-url-javascript

You can then use it like the PHP $_GET , but with parentheses instead of brackets. 然后,您可以像PHP $_GET一样使用它,但使用括号而不是括号。

I would use a function like below 我会使用下面的函数

function fetchGetVariables() {
  var loc = "http://www.test.co.uk/search-restaurant?location=asokoro&day=today&time=1100";
  // var loc = window.location.href; // Use this in actual use
  var result = {};

  var parts = loc.split("?");

  if (parts.length > 0) {
    var params = parts[1].split("&");

    for (var i = 0; i < params.length; i++) {
      var keyValuePair = params[i].split("=");

      var key = keyValuePair[0];
      var value = "";
      if (keyValuePair.length > 0) {
        value = keyValuePair[1];
      } 

      result[key] = value;
    }
  }

  return result;
}

console.log(fetchGetVariables());

This gives the output: 这给出了输出:

Object {location: "asokoro", day: "today", time: "1100"}

See it at this fiddle. 这个小提琴看到它。

If you are using angular you can pull the values from the URL with the $routeParams. 如果使用angular,可以使用$ routeParams从URL中提取值。

https://docs.angularjs.org/api/ngRoute/service/$routeParams#! https://docs.angularjs.org/api/ngRoute/service/$routeParams#!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM