简体   繁体   English

来自url的角度,编码和解码对象

[英]Angular, encode and decode object from url

I have an object I'm trying to pass by url - something like 我有一个我试图通过url传递的对象 - 类似于

$location.search(myObject) 

However when this object becomes an array of objects I have a problem getting it there. 但是当这个对象成为一个对象数组时,我遇到了问题。 I'm wondering how I can put up a large object (right now it's an array of objects, I don't know if that needs to change), and then pull it back down into a controller with an 我想知道如何建立一个大对象(现在它是一个对象数组,我不知道是否需要更改),然后将其拉回到一个控制器中

 $location.search()

This works fine if my object is just 1 small object, right now its an array of objects with levels inside like so 如果我的对象只是一个小对象,那么它的工作正常,现在它的内部级别的对象数组就像这样

 var testIt = [{
                    "module1" : 
                    [
                        { 
                            "name1"  : ["option1", "option2", "option3"]
                        },
                        { 
                            "name2"  : ["option1", "option2", "option3"]
                        }
                    ]
                },
                {
                    "module2" : 
                    [
                        { 
                            "name3"  : ["option1", "option2", "option3"]
                        },
                        { 
                            "name4"  : ["option1", "option2", "option3"]
                        }
                    ]
                }];

            $location.search(testIt);

How can I work with something like this, (it's ok if the url is huge right now, but if there is a way to shrink it - even better!) 我怎样才能使用这样的东西,(如果网址现在很大,那就好了,但如果有办法缩小它 - 甚至更好!)

Thanks! 谢谢!

I would make a service to encode/decode an object into a URL encoded JSON string: 我会提供服务来将对象编码/解码为URL编码的JSON字符串:

angular.module('app', [])
.factory('StateString', function() {
  return {
    encode: function(data) {
      return encodeURIComponent(JSON.stringify(data));
    },
    decode: function(searchString) {
      return JSON.parse(decodeURIComponent(searchString));
    }
  };
});

Then you can put that JSON string on a query parameter on the URL like so (be sure to inject the StateString service we just defined: 然后你可以将这个JSON字符串放在URL上的查询参数上(确保注入我们刚刚定义的StateString服务:

var data = [
  {
    module1: {
      name1: ["option1", "option2"]
    }
  }, {
    module2: {
      name2: ["option2", "option2"]
    }
  }
];

$location.search({
  state: StateString.encode(data)
});

And fetch it back like so: 然后像这样取回它:

var state = StateString.decode($location.search().state);

Your URL will be pretty long, the example I have given produces a query string of: 您的URL将很长,我给出的示例生成一个查询字符串:

"%5B%7B%22module1%22%3A%7B%22name1%22%3A%5B%22optio…22%3A%5B%22option2%22%2C%22option2%22%5D%7D%7D%5D"

I'm sure someone has some bright ideas on how you can compress it... 我相信有人会对你如何压缩它有一些好主意...

EDIT 编辑

If you wanted, you could include the $location.search() parts into your service: 如果您愿意,可以将$location.search()部分包含在您的服务中:

angular.module('app', [])
.factory('LocationSearchState', function() {
  return {

    set: function(data) {
      $location.search(
        { state: encodeURIComponent(JSON.stringify(data)) }
      );
    },

    get: function(searchString) {
      return JSON.parse(
        decodeURIComponent($location.search().state)
      );
    }

  };
});

So in your controller (or wherever), you could just use: 所以在你的控制器(或任何地方),你可以使用:

var state = [{ ... }];
LocationSearchState.set(state);

and

var state = LocationSearchState.get();

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

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