Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert $.param in angularjs

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

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

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

can anyone help me how to do this in angularJs

like image 265
vintot Avatar asked Jun 17 '14 03:06

vintot


People also ask

What is params in Angularjs?

Angularjs routeParams is a service that allows you to retrieve the current set of route parameters(URL Parameters). It Requires the ngRoute module to be installed. The route parameters are a combination of $location's search() and path(). The path parameters are extracted when the $route path is matched.

How to use param in AngularJS?

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.


2 Answers

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

like image 148
Rafal Zajac Avatar answered Sep 27 '22 16:09

Rafal Zajac


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

like image 33
Sudhir Bastakoti Avatar answered Sep 27 '22 15:09

Sudhir Bastakoti