Convert $.param in angularjs

后端 未结 6 1635
梦如初夏
梦如初夏 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: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

提交回复
热议问题