使用AngularJS解析查询字符串而不使用html5mode的最佳方法

 mobiledu2502906557 发布于 2023-02-09 09:34

使用html5mode的情况下解析Angular中的查询字符串的最佳方法是什么?(不使用html5mode,因为我们需要支持旧版浏览器)

无论我是否使用哈希,我都得到相同的未定义结果:

http://localhost/test?param1=abc¶m2=def
http://localhost/test#/param1=abc¶m2=def

$ routeParams和$ location.search()都返回undefined:

var app = angular.module('plunker', ["ngRoute"]);

app.controller('MainCtrl', ["$scope", "$routeParams", "$location",
  function($scope, $routeParams, $location) {

  console.log($routeParams, $routeParams.abc); //undefined, undefined
  console.log($location.search(), $location.search().abc); //undefined, undefined

}]);

我可以自己解析window.location.search,但我希望在Angular中有更好的方法.

Plnkr:http://plnkr.co/edit/alBGFAkfqncVyK7iv8Ia?p=preview

我看过这篇文章并没有找到解决方案.我肯定错过了什么.谢谢您的帮助.

1 个回答
  • 查询参数应该在哈希之后:

    http://localhost/test#/?param1=abc&param2=def

    这应该允许$location.search()返回一个像这样的对象:

    {
      param1: 'abc',
      param2: 'def'
    }
    

    2023-02-09 09:39 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有