在 angularjs 中转换 $.param [英] Convert $.param in angularjs

查看:32
本文介绍了在 angularjs 中转换 $.param的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用 JQuery 之前,我用它来发送带参数的 URL

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

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

但是对于 angularjS,这不一样

but with angularjS this does not work the same way

$scope.myButton = function() {
    $window.location.open = myUrl + $.param({"paramName" : "ok","anotherParam":"hello"});
};//Error: $ is not defined

谁能帮助我如何在 angularJs 中做到这一点

can anyone help me how to do this in angularJs

推荐答案

如果您正在尝试创建数据的序列化表示,例如 $.param() 确实如此,

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

这篇关于在 angularjs 中转换 $.param的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆