angularjs $http params with slash - angularjs

I am using angularjs with slim php and I have a GET action in slim which
action/param1/param2
Then in my angularJS
$http({
url: 'action',
method: 'GET',
params:{param1: param1, param2: param2}
}).success();
It return error in console with using ? and &, but I need it get with slash between params
http://xxxxxx/action?param=aaa&param2=bbb
What I have tried, I change the $http params to below and it works
url: 'action/' + param1 + '/' + param2
Ok, my question is, is there any better solution compare to mine? because I feel mine stupid.

Use $resource instead:
var myResource = $resource('action/:foo/:bar', {},
{ get: { method: 'GET' } }
);
myResource.get({foo:1, bar:2});
Will yield a call to:
http://yourserver.com/action/1/2

Related

Angular $resource: object id lost using POST with custom methods

I want to use a custom method in my Foo Factory with a POST call. When I call it as a GET request, it works, but it's deny by my api because it doesn't allow GET for that method.
Request: GET /api/foo/272ee694-b517-4012-b740-98f76973091d/custom_method/ 405
When I change the method to POST (in the angular factory), the object id disapears:
Request: OPTIONS /api/foo/custom_method/ 174
Request: POST /api/foo/custom_method/ 405
Note that this time an OPTION request is made before without the id.
My factory looks like:
appService.factory('foo', ['$resource', 'appConfig',
function($resource, appConfig){
var api_path = appConfig.api_path;
return $resource(api_path + 'foos/:fooId/', {fooId:'#id'},{
query: {
method: 'GET',
isArray: false
},
customMethod: {
method: 'POST',
url: api_path + 'foos/:fooId/custom_method/'
}
});
}]);
And my controler:
$scope.foo = foo.get({fooId: $routeParams.fooId});
$scope.customMethod = function() {
foo.customMethod({fooId:$scope.foo.id});
}
Anyone could help me? Thank you in advance
So, I found the solution. In the controller, just call custom_method from the object with a $. Like this:
$scope.foo.$custom_method();
You can also omit to pass the id because $resource get it from the object by default.

update factory value when controller data changed

i have a factory in my app:
dashboard.factory('ProxyStatusFactory', ['$resource','CodisData', function ($resource,CodisData) {
return $resource('http://' + CodisData.Addr + '/api/proxy', {}, {
query:{ method: 'GET', url : 'http://' + CodisData.Addr + '/api/proxy/list', isArray: true },
setStatus:{ method: 'POST' }
});
}]);
the CodisData is a angular value
dashboard.value("CodisData",{"Name":"NA","Addr":"NA"});
then i use it in my controller like this:
$scope.codis_name = selected.Tag;
$scope.codis_addr = selected.Addr;
CodisData.Addr = selected.Addr;
$scope.proxy_array = ProxyStatusFactory.query();
i changed CodisData.Addr ,but in ProxyStatusFactory ,$resource also return 'http://na/api/proxy'
so when CodisData change,how synchronized update ProxyStatusFactory and return correct $resource?
try this:
dashboard.factory('ProxyStatusFactory', ['$resource', function ($resource) {
return $resource('http://:addr/api/proxy/', {}, {
query: {
method: 'GET',
url: 'http://:addr/api/proxy/list',
params: {},
isArray: true
},
setStatus:{ method: 'POST' }
});
}]);
then use in controller like this:
ProxyStatusFactory.query({addr: CodisData.Addr});
Moving forward with explanation...
angular factory/service are singletons, so they are created only ONCE. So http url is also created only once, based on current value of CodisData.Addr.
Thats why + CodisData.Addr + is changed to :addr to make this part of url configurable....
I hope this helps :)
#update
tested on local project and injecting variables into url part, works as expected, but when desired url is external rest api then it fails with message you describe.
In that case You can dynamically create $resource object. Here is jsfiddle with working factory that creates resources dynamically.
https://jsfiddle.net/qwetty/mxrz2cxh/13/

how to change url dynamically when using $ngResource in angularJs

Here is my code
Const User = $resource('/user/:id', {},
{
'query': {
method: 'GET',
url:'/user/phone/:phone/name/:name',
isArray: true
}
});
When I type:
User.query({name: Li});
User.query({phone: 123456});
Http request will be:
get /user/phone/name/Li
get /user/phone/123456/name
But I hope it to be:
get /user/name/Li
get /user/phone/123456
Could someone tell me how to remove part substring in url dynamically? Must I use $http to generate different url respectively?
My English is poor. Thanks!

$resource send data in get insted of post

I am try to post data form $resource in angular js project. It is my code in factory
deletemenu: function(menudata ) {
return $resource(url+'/menu/delete', {}, {
delete_menu: {
method: 'POST',
params: {
entityType : menudata.entityType ,
sellerId : menudata.sellerId ,
menuId : menudata.menuId ,
parentMenuIds : menudata.parentMenuIds ,
menuItemId : menudata.menuItemId
},
isArray: false
}
}).deletemenu();
}
I declare method POST but the request is send as a query string not the post request. It send as a
URL+"menu/delete?entityType=I&menuId=25&menuItemId=20&parentMenuIds=4&sellerId=1"
How I send these data as a POST method.
I think this should work better for you
return $resource(url + '/menu/delete', null, {
delete_menu: {method: 'POST'}
}).delete_menu(menudata);
Here, I'm passing in the menudata as the first argument to the non-GET "class" action delete_menu which will use it as postData.
The objects returned by $resource are much more flexible than what you're using here. It looks like you should be able to make your service / factory much neater by returning the $resource instance instead of these individual functions. For example
.factory('menuFactory', function($resource) {
var url = 'whatever';
return $resource(url, null, {
delete: {
url: url + '/menu/delete',
method: 'POST'
} //, other actions here
});
})
Then you can call it with
menuFactory.delete(menuData)

$http.get with parameters not working

I am trying to send a GET request using AnguarJS $http.get function.
However, the shorthand version is NOT working. Why is that?
Working:
$http({
url: $rootScope.root + '/brands',
method: 'GET',
params: postData
}).success(function(data) {
console.log(data);
});
Not working:
$http.get($rootScope.root + '/brands', postData).success(function(data) {
console.log(data);
$scope.brands = data;
});
The 2nd parameter for the $http.get shortcut method is meant to pass in parameters such as cache control, etc. Only for $http.post does the 2nd parameter accept the post data. If you are using the shortcut for $http.get you will need to pass in the query parameters as part of the URL: $http.get($rootScope.root + '/brands?a=1&b=2')
Ref: http://docs.angularjs.org/api/ng.$http#methods_get

Resources