angularjs factory with $resource doesn't work - angularjs

This is my angularjs factory :
app.factory('lordREST', ['$resource', function($resource){
return {
myPost: function(dataSearch, dataParam) {
$resource(
urlLordRest,
{search: dataSearch, param: dataParam},
{'query': {method: 'GET', isArray: true, cache: false, headers: {'Accept': 'application/json'}}}
);
}
}
}]);
and in my controller i use it like that :
lordREST.myPost({dataSearch: dataSearch, dataParam: dataParam}, function(responce) {
$scope.posts = responce;
// CONSOLE LOG CONTROL
// console.log(defineCLC + "data changed");
console.log($scope.posts);
});
But this action return nothing, what's wrong ?

Related

Cannot use $http within angular factory (angular 1.x)

I have added $http at the start of the script but for some reason the $http isn't loaded - how do I log $http into the module rather than the controller
var abcdReportServices = angular.module('abcdReportServices', [ ]);
abcdReportServices.factory('uploadFileAjax', ['getPDFsImage', function(getPDFsImage) {
return function(evt, $scope){
$scope.http({
method: "POST",
url: 'someEndpoint/doSomething',
data: $.param({
'name': 'name-of-rpt'
}),
headers: {
"Content-Type" : "application/x-www-form-urlencoded"
}
}).success(
function(data) {
console.log("Saving success", data);
}
);
}
}
}]);
// error in console log
$http is not defined...
You just need to include $http in your factory's dependency injection and then switch your $scope.http to $http.
var abcdReportServices = angular.module('abcdReportServices', [ ]);
abcdReportServices.factory('uploadFileAjax', ['getPDFsImage', '$http', function(getPDFsImage, $http) {
return function(evt){
$http({
method: "POST",
url: 'someEndpoint/doSomething',
data: $.param({
'name': 'name-of-rpt'
}),
headers: {
"Content-Type" : "application/x-www-form-urlencoded"
}
}).success(
function(data) {
console.log("Saving success", data);
}
);
}
}
}]);

remove cachebuster in AngularJs API call

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.

Query methods in angularJs

How can I make query from function VisitService to get date method "from" "to" and show in my html page.
Is this code will work good ??
Is better with explainition
(function() {
'use strict';
angular
.module('projectsEvaluationApp')
.factory('EvaluationsService', EvaluationsService)
.factory('VisitService', VisitService)
.factory('WebPagesService', WebPagesService);
EvaluationsService.$inject = ['$resource'];
VisitService.$inject = ['$resource'];
WebPagesService.$inject = ['$resource'];
function EvaluationsService ($resource) {
var service = $resource('api/evaluations/:id', {}, {
'get': {
method: 'GET',
isArray: true
},
'query': {
method: 'GET',
isArray: true,
}
});
return service;
}
function VisitService ($resource) {
var service = $resource('api/visits/:id', {}, {
'get': {
method: 'GET',
isArray: true
},
'query': {
method: 'GET',
isArray: true,
}
});
return service;
}
function WebPagesService ($resource) {
var service = $resource('api/web-pages/:id', {}, {
'get': {
method: 'GET',
isArray: true
},
'query': {
method: 'GET',
isArray: true,
}
});
return service;
}
})();
Thank you
First of to use $resource you need to install and inject ng-resource
You can use like this
function VisitService ($resource) {
return $resource('api/visits/:id', {}, {
}).query();
}
Default query method is GET with default isArray as true
If you want to pass a query parameter
Your API will be
function VisitService (fromDate,toDate) {
return $resource(''api/visits?fromDater' + fromDate + '&toDate' + toDate', {}, {
}).query();
}
Call it in controller
$scope.test = VisitService('01/04/2016','01/04/2016');
User in HTML
{{test}}

using ngResource factory in controller showing error

hi everyone I am new with angular js I want to get data through getting method using Resource I have to build a resource factory but when I call this factory in controller I got this error Error: Users.myUser is undefined Please anyone can tell what I have done wrong or why I am getting this error here is my code.
var Myapp = angular.module('starter.controllers', ['ngResource'])
.config(['$resourceProvider', function ($resourceProvider) {
$resourceProvider.defaults.stripTrailingSlashes = false;
}]);
Myapp.factory('Users', function ($resource) {
return{
myUser:
$resource('some url', {}, {
query: {
method: 'POST',
params: {},
isArray: false
}
})
};
});
Myapp.controller('DashCtrl', function ($scope,Users) {
Users.myUser.query().$promise.then(function (data) {
console.log(JSON.stringify(data, null, 4));
}, function (error) {
console.log('Error is: ' + JSON.stringify(error, null, 4));
});
});
You should return $resource object from myUser factory function.
Myapp.factory('Users', function($resource) {
return {
myUser: function() {
return $resource('some url', {}, {
query: {
method: 'POST',
params: {},
isArray: false
}
});
}
}
});
Controller
Myapp.controller('DashCtrl', function ($scope,Users) {
//Call myUser method like below
Users.myUser().query().$promise.then(function (data) {
console.log(JSON.stringify(data, null, 4));
}, function (error) {
console.log('Error is: ' + JSON.stringify(error, null, 4));
});
});
Update your factory as below
Myapp.factory('Users', function ($resource) {
function myUser() {
return $resource('some url', {}, {
query: {
method: 'POST',
params: {},
isArray: false
}
})
}
});

Dynamic URL for $resource in an angular factory without using global variable

How can I define a dynamic factory in such a way that the URL is passed from the controller without using $rootScope?
.factory('getData', function ($resource,$rootScope) {
return $resource($rootScope.url, {id:'#id'}{
'query': { method: 'GET', isArray: true},
'get': {
method: 'GET',
isArray: true,
transformResponse: function (data) {
data = angular.fromJson(data);
return data;
}
}
});
})
Controller
var data = [];
$rootScope.url='/userDetails/userId?userId=userID'
getData.get({id:'123'}).$promise.then(function(data){
angular.forEach(data,function(dataVal){
//
},data)
You should create a function in your factory and then pass the URL and id as parameters to that function when you invoke it.
For your code the factory should look something like this:
.factory('getData', function($resource, $rootScope) {
return {
query: function(url, id){
return $resource(url, {userId: id}, {
'query': {
method: 'GET',
isArray:true
},
'get': {
method: 'GET',
isArray:true,
transformResponse: function(data) {
console.log(data);
data = angular.fromJson(data);
return data;
}
}
}).get();
}
}
})
And then you would invoke it like this getData.query(url,id) in the controller.

Resources