Convert $.param in angularjs

后端 未结 6 1627
梦如初夏
梦如初夏 2020-12-06 16:02

Before I am using JQuery and I use this to send URL with parameter

window.location = myUrl + $.param({\"paramName\" : \"ok\",\"anotherParam\":\"hello\"});


        
相关标签:
6条回答
  • 2020-12-06 16:30

    You can simply use $.param on the javascript object and pass it to either $resource or $http and it should work fine. One caveat though is ensure it is an object and not an array.

    var badParam = {'name':'john',...}; // contains more properties
    var goodParam = {name :'john',...}; // contains more properties
    
    0 讨论(0)
  • 2020-12-06 16:32

    There's a built-in serializer in angular which mimics $.param(): $httpParamSerializerJQLike

    0 讨论(0)
  • 2020-12-06 16:32

    you can inject and use this function instead: $httpParamSerializerJQLike()

    0 讨论(0)
  • 2020-12-06 16:40

    If you are trying to create serialized representation of data like $.param() does,

    function serializeData( data ) { 
        // If this is not an object, defer to native stringification.
        if ( ! angular.isObject( data ) ) { 
            return( ( data == null ) ? "" : data.toString() ); 
        }
    
        var buffer = [];
    
        // Serialize each key in the object.
        for ( var name in data ) { 
            if ( ! data.hasOwnProperty( name ) ) { 
                continue; 
            }
    
            var value = data[ name ];
    
            buffer.push(
                encodeURIComponent( name ) + "=" + encodeURIComponent( ( value == null ) ? "" : value )
            ); 
        }
    
        // Serialize the buffer and clean it up for transportation.
        var source = buffer.join( "&" ).replace( /%20/g, "+" ); 
        return( source ); 
    }
    

    and use this for your data serialization

    0 讨论(0)
  • 2020-12-06 16:45

    AngularJs has jquery lite on its core so you can use angular.element.param() instead of $.param()

    0 讨论(0)
  • 2020-12-06 16:52

    I found this function useful for url serialization. Will also work for nested objects.

    var param = function(obj) {
    
      if ( ! angular.isObject( obj) ) { 
        return( ( obj== null ) ? "" : obj.toString() ); 
      }
      var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
    
      for(name in obj) {
    
        value = obj[name];
        if(value instanceof Array) {
          for(i in value) {
    
            subValue = value[i];
            fullSubName = name + '[' + i + ']';
            innerObj = {};
            innerObj[fullSubName] = subValue;
            query += param(innerObj) + '&';
          }
    
        } else if(value instanceof Object) {
          for(subName in value) {
    
            subValue = value[subName];
            fullSubName = name + '[' + subName + ']';
            innerObj = {};
            innerObj[fullSubName] = subValue;
            query += param(innerObj) + '&';
          }
        }
        else if(value !== undefined && value !== null)
          query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
      }
    
      return query.length ? query.substr(0, query.length - 1) : query;
    };
    
    0 讨论(0)
提交回复
热议问题