I want genereate url in angular resource using FOSJSBundle.
This is the code that need FOSJSBundle to generate url:
Routing.generate('get_constructionprivateinformation_industrialists', {id: id}, false));
This is the service code that use resource:
services.factory('IndustrialistsFactory', function ($resource) {
return $resource(
'/app.php/api/v1/constructionprivateinformations/:id/industrialists',
{id: '#id'},
{
query: { method: 'GET', isArray: true },
create: { method: 'POST'}
}
)
});
This code works but I want generate url using FOSJSBundle, then If I do this code not works because Routing.generate have not access to id.
I proved this code but not works:
services.factory('IndustrialistsFactory', function ($resource) {
return $resource(
Routing.generate('get_constructionprivateinformation_industrialists', {id: id}),
{id: '#id'},
{
query: { method: 'GET', isArray: true },
create: { method: 'POST'}
}
)
});
What Can I do?
Thanks
Related
I am new to AngularJS and I have a question here.
I am using $resource for my CRUD actions.
I currently have the code like this,
angular.module("dopAngular.services")
.factory("UserRoleService", ["$resource",
function ($resource) {
return $resource("api/UserRoleApi", {}, {
query: { method: "GET", isArray: true },
create: { method: "POST" },
get: { method: "GET" },
remove: { method: "DELETE" },
update: { method: "PUT" }
});
}]);
//below is the code in my controller
UserRoleService.query(function (data) {
vm.UserRoleLookups = data;
});
I would like to make my UserRoleService generic, which means I don't want to provide the specific URL for the API in the factory level.
I now modify my code a little bit,
angular.module("dopAngular.services")
.factory("UserRoleService", ["$resource",
function ($resource, url) {
return $resource(url, {}, {
query: { method: "GET", isArray: true },
create: { method: "POST" },
get: { method: "GET" },
remove: { method: "DELETE" },
update: { method: "PUT" }
});
}]);
My question is what I should do in my controller?
So, instead of directly returning $resource, we can encapsulate it with a function that accepts url as a param.
Something like this:
myApp.factory('UserRoleService', function($resource) {
return {
query: function(url) {
return $resource(url, {}, {
query: {
method: "GET",
isArray: true
},
get: {
method: "GET"
}
});
}
}
});
Now, in controller, you can call it like:
UserRoleService.query('//httpbin.org').get()
example fiddle
I am using ngCacheBuster in my app.js and
angular.module('myApp')
.factory('User', function ($resource) {
return $resource('api/users/:email', {}, {
'query': {method: 'GET', isArray: true},
'search':{
url:'api/search',
method:'GET',
params: {
email: '#email'
},
isArray:true
}
});
});
and calling as
User.search({email:$scope.nameQuery},function(result,headers){
$scope.users = result;
});
when I am trying to call this method, it is showing as
http://localhost:3000/myApp/api/search?cacheBuster=1488208210950&email=searchString
is there a way to remove cacheBuster from my url
I want something like
http://localhost:3000/myApp/api/search?email=searchString
Any help would be appreciated.
I am beginner learning Angularjs .Please help me with examples for following
script added
javascript -
var app = angular.module('myapp', []);
app.controller('MyCtrl1', ['$scope', 'UserFactory', function ($scope, UserFactory) {
UserFactory.get({}, function (userFactory) {
$scope.time = userFactory.time;
})
}]);
var service = angular.module('apiService', ['ngResource']);
service.factory('UserFactory', function ($resource) {
return $resource('http://time.jsontest.com', {}, {
query: {
method: 'GET',
params: {},
isArray: true
}
})
});
.html file
<body ng-app="myapp">
<divng-controller="MyCtrl1" >
<p>
Result from RESTful service is: {{ time }}
</p>
</div>
</body>
above snippet gives the out put as
Result from RESTful service is : {{time}}
and not the value i am expecting
..Reference : http://draptik.github.io/blog/2013/07/13/angularjs-example-using-a-java-restful-web-service/
I want to write CRUD methods (GET/POST/PUT/DELETE) and I have started with GET.
Thanks
You need to make sure that your main app module injects your service. In your plnkr you have:
var app = angular.module('myapp', []);
where you should really have:
var app = angular.module('myapp', ['apiService']);
This ensures that the service module is injected into your app module, and you can use the UserFactory that you define in that module. For this simple case you could have also simply defined the UserFactory factory on the 'myapp' module as well
It's very close but you have a slight mistake in your app instantiation. It should be the following:
var app = angular.module('myapp', [ 'apiService' ]);
There's a couple other issues I see as well but one thing is I usually do the following for async requests
var promise = UserFactory.get({}).$promise;
promise
.then( function(response) {
$scope.time = userFactory.time;
});
EDIT: Here's an example for named methods for a given ReST service:
return $resource('/api/v2.0/user/lists/:listId',
{},
{
// POST - list create/product addition to list
'addProduct': {
method: 'POST',
isArray: false,
params: {
listId: '#listId',
productId: '#productId'
}
},
'createList': {
method: 'POST',
isArray: false,
params: {
listName: '#listName'
}
},
// GET - list of user lists/list details
'readLists': {
method: 'GET',
isArray: false,
params: {}
},
'readListsWithProductId': {
method: 'GET',
isArray: false,
params: {
productId: '#productId'
}
},
'readListById': {
method: 'GET',
isArray: false,
params: {
listId: '#listId',
sort: '#sort',
flags: true,
extendedInfo: true,
rows: '#rows',
start: '#start'
}
},
// PUT - list renaming
'renameList': {
method: 'PUT',
isArray: false,
params: {
newName: '#listName',
listId: '#listId'
}
},
// DELETE - list deletion/clear/product removal
'removeProduct': {
method: 'DELETE',
isArray: false,
params: {
listId: '#listId',
productId: '#productId'
}
},
'clearList': {
method: 'DELETE',
isArray: false,
params: {
listId: '#listId',
clear: true
}
},
'deleteList': {
method: 'DELETE',
isArray: false,
params: {
listId: '#listId'
}
}
});
You could access it like the following:
Factory.[methodName](payload)
I have searched other solutions but it doesn't work, solution was to put isArray: true, but it doent work. I am working with objects btw. I am getting this message in console, but everything works, data has been send to my backend api. How to remove that error ?
app.factory('Category', function($resource, $http) {
return $resource('http://localhost/api/v1/category/', {id: "#id"},
{
update: {
method: 'POST',
isArray: false
},
save: {
method: 'PUT'
},
query: {
method: 'GET',
params: {id: '#id'},
isArray: false
},
create: {
method: 'POST',
headers: {'Content-Type': 'application/json'}
},
drop: {
method: 'DELETE',
params: {id: '#id'}
}
}
);
});
Here is my function
$scope.createInventory = function(){
Inventory.create($scope.newinfo);
$scope.info.push(Inventory);
};
$scope.info not a array. You must specify $scope.info to array, then you can push any values to $scope.info.
Try this
$scope.info=[];
$scope.createInventory = function(){
Inventory.create($scope.newinfo);
$scope.info.push(Inventory);
};
I want to delete an object selected by id, but when I press "delete" all objects are removed.
If I specify Inventory.drop({id: 2} it works, but using Inventory.drop({id: '#id'}) it doesn't. Why?
$scope.deleteInv = function () {
Inventory.drop();
};
My template:
<td>X</td>
Here is my factory:
app.factory('Inventory', function($resource, $http) {
return $resource('http://someweb.com/api/v1/inventory/:id', {id: '#id'},
{
update: {
method: 'POST',
params: {id: '#id'},
isArray: false
},
save: {
method: 'PUT'
},
query: {
method: 'GET',
params: {id: '#id'},
isArray: false
},
create: {
method: 'POST'
},
drop: {
method: 'DELETE',
params: {id: '#id'}
}
}
);
});
Because your #id is undefined.
Where you assign values for that? I think you does't.
So you need to change that
params: {id: $scope.YourId}
and you need to should be pass any values for $sope.YourId
and also You need define $sope in your factory app.factory('Inventory', function($resource, $http,**$scope**) {