首页 > 代码库 > 用angular实现$.param()

用angular实现$.param()

首先介绍一下$.param()

功能: 序列化对象或数组,返回字符串

eg:

var params = { width:1900, height:1200 };
var str = jQuery.param(params);
console.log(str);

输出: width=1680&height=1050

使用angular替代,方法为:

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

 

用angular实现$.param()