get paramters from url without key in angularjs - angularjs

I'am trying to get paramter from url like that:
index.html#!/forum/page?1-something
the number that what i'am trying to get
both $location.url(); and $location.absUrl(); get the whole url
do I have to use split and how to do that?

Using $location.search() you can get all query parameters.
by iterating an object you can get all key and value as well.
I used to iterate object using for-in loop. here $location.search() is an object itself so you can iterate it as:
for(let key in $location.search()){
console.log("key: "+key+" value: "+$location.search()[key]);
}
we can also append query parameters using $location.search() by just passing parameters to it. this can be done as:
let obj = {site:'stack overflow', exp: 'happy coding'}
and this obj object can be passed to $location.search() as:
$location.search(obj);

Related

Passing array value to a query string url

I have an array of id let's say
favorites = [102,110,112,125]
I want to retrieve the corresponding object for each id by passing it to query string like this :
public getFavorites(favs){
let favorites = favs.join();
let encodedUrl = encodeURIComponent(JSON.stringify({"id": favorites }));
return this.http.get(this.api_url + '/1/query/data/getFavs?parameters='+encodedUrl, {
headers: this.authHeader
})
.retry(3)
.map(res => res.json());
}
The problem is only one object appear in my html template and also in the console. What is the best way for me to pass an array of value to a URL as parameters in order to retrieve the associated objects?
You can pass multiple parameter values with the same name over the querystring. Does that help you? For example, here's a snippet:
this.api_url + '/1/query/data/getFavs?id=101&id=110
Here is another answer that has some more info on this.
If you have to send the ID's over in a serialized manner, consider posting the JSON instead of using the GET method. If you're trying to maintain adherence to REST verb standards by making it a get call, can you post the server code?

Angular $resource to get several records

I have the following ids:
var myIds = ['somelongId', 'someotherlongid'];
I want to get only the records that correspond from mongo (I am using angular-fullstack) using $resource.
My factory
angular.module('myApp')
.factory('MyFactory',function ($resource) {
return $resource('/api/things/:id');
});
My faulty code returns all the records
MyFactory.query(myIds);
You should query correctly, by passing id parameter in it. You can't pass array object inside query, if you try to pass that then that will make stringify that array to [Object Object] & it will throw an error.
Instead I'd suggest you to pass any one of id to it.
MyFactory.query({ id: myIds[0] });
Edit
If you wanted to pass multiple ids in a query statement then pass it as , separated string and then do split out that key on server side. In that way you could pass multiple array to server & will get array of response(.query will make thing done).
MyFactory.query({ id: myIds.join(',') });
This will create url as
http://localhost/api/things/somelongId,someotherlongid

AngularJS Restangular: How to remove default request param from base url

EC2Restangular.setBaseUrl('localhost:9000/');
EC2Restangular.setDefaultRequestParams({
"Id": ID
});
Is there any way to remove default query params from baseUrl in Restangular?
To remove the parameters you can just pass an empty object:
EC2Restangular.setDefaultRequestParams({});
If you want to remove the request parameters from a single request, you can add the empty object to the method call like so:
Restangular.all("widget").getList({})
Update: To remove a specific paramter from the default parameter set is a little trickier.
We can grab the parameter list from the Restangular object, and then use that to remove the parameter we don't need, and then use that new parameter object to pass to the new request:
var params = Restangular.all("projects").reqParams;
delete params['parameterNameYouWantToRemove'];
var projects = Restangular.all("projects").getList(params);

How to create complex query parameters in Restangular

I need to create a fairly complex query string in Restangular.
http://vdmta.rd.mycompany.net//people?anr=Smith&attrs=givenName,displayName,name,cn
How do I do this?
So far I am OK getting as far as ?anr=Smith using this:
return Restangular.all('/people').getList({anr:searchTerm});
The last part attrs=x,y,x lets me control which attributes I want back in the search and could change per request I make.
Any advice appreciated.
Regards
i
You should be able to simply add another query parameter where the value is your comma separated list of attributes.
var attributes = ['givenName' , 'displayName']; // attributes you require for the request
var attributesAsString = attributes.join();
return Restangular.all('/people').getList({
anr : searchTerm,
attrs: attributesAsString
});

ng $resource Fails to Dynamically Generate URI

The Problem:
I have code to set up a $resource that seems to compile without error:
var ReportsRest = $resource('/reports/:reportId', {reportId: '#id'});
ReportsRest.get({id: 123});
but when that code actually executes the request url that is generated looks like this:
GET http://localhost:5523/reports?id=123 404 (Not Found)
The id is not being parsed and dynamically loaded into the URI. Is there something that I am missing?
Plunkr
http://plnkr.co/edit/3ikqMsnDI9r9LThaQLf3?p=preview
Try this:
var ReportsRest = $resource('/reports/:reportId', {reportId: '#id'});
ReportsRest.get({reportId: 123});
Under $resource Usage, there is this block of text.
paramDefaults
Default values for url parameters. These can be overridden in actions methods. If any of the parameter value is a function, it will be executed every time when a param value needs to be obtained for a request (unless the param was overridden).
Each key value in the parameter object is first bound to url template if present and then any excess keys are appended to the url search query after the ?.
Given a template /path/:verb and parameter {verb:'greet', salutation:'Hello'} results in URL /path/greet?salutation=Hello.
If the parameter value is prefixed with # then the value for that parameter will be extracted from the corresponding property on the data object (provided when calling an action method). For example, if the defaultParam object is {someParam: '#someProp'} then the value of someParam will be data.someProp.
To summarize, the object you pass in needs to use the key of your paramDefaults object to get the value. That value replaces the same :key text in your URL pattern. Any value in the paramDefaults object that is prefixed with an # will be set on the returned model data as a property named whatever follows the #.
var ReportsRest = $resource('/reports/:reportId', {reportId: '#id'});
ReportsRest.get({
reportId: 123,
other: 123
}, function(data) { /* ... */ });
This makes a request to /reports/123?other=123. data, in the callback, looks like { id: 123 }.

Resources