Custom function on angular $resource giving error "is not a function" - angularjs

Service
sampleApp.factory('Order', function ($resource, $cacheFactory) {
return $resource('http://example.com/api/v1/orders/:id', {id: '#id'},
{
'query': {method:'GET', cache: true},
'get': {method:'GET'},
'updateRow': {method:'POST', params:{charge:true}},
'deleteRow': {method:'POST', params:{charge:true}},
'changeStatus': {method:'POST', params:{charge:true}}
});
});
Controller:
$scope.updateRow = function(od){
var order = od;
order.$updateRow();
}
Order.get() and Order.query() works fine. but when i try to call my custom function it is giving error: order.$updateRow is not a function
Can anyone help why is this error?

try this,
$scope.updateRow = function(od){
Order.updateRow({id: od});
}

Related

Unable to get result of asynchronous call in the controller $scope

I was trying to improve the code in my application by moving the RESTful call in my angularJS to a factory like so:
In app.js
var myApp = angular.module('myApp', ['ngRoute','ngResource']);
In services.js
swof.factory('engineerService', function($resource)
{
var data = $resource('/api/engineers/:empid',{empid: "#empid"},
{
'get':
{ method:'GET'},
'save':
{method:'POST'},
'query':
{method:'GET',
transformResponse: function(data)
{
return angular.fromJson(data);
},
isArray:true},
'remove':
{method:'DELETE'},
'delete':
{method:'DELETE'}
});
return data;
});
In controller.js
engineerService.query().$promise.then(function(data)
{
$scope.engineers = data;
typeof $scope.engineers;
console.log("[in]", $scope.engineers);
$scope.$apply;
});
console.log("[out]", $scope.engineers);
The controller is referenced in the engineer.html page and the idea is that it will display a list of engineers from $scope.engineer.
The output in the Chrome console is as follows:
For some reason I am unable to get the data returned from the REST query to appear outside of the section of the controller with the $promise.
Can someone please help me with this?
Here is the solution. First of all the services part:
swof.factory('engineerService', function($resource)
{
var data = $resource('/api/engineers/:empid',{empid: "#empid"},
{
'get':
{ method:'GET'},
'save':
{method:'POST'},
'query':
{method:'GET',
transformResponse: function(data)
{
return angular.fromJson(data);
},
isArray:true},
'remove':
{method:'DELETE'},
'delete':
{method:'DELETE'}
});
return data;
});
Now for the controller:
swof.controller('engineerController', ['$scope', '$log', '$http', 'engSchedService', 'engineerService', function($scope, $log, $http, engSchedService, engineerService ) {
engineerService.query().$promise.then(function(data)
{
$scope.engineers = data;
});
}]);
The values in $scope.engineers was accessed in html using ngrepeat as follows:
<tbody ng-repeat="engineer in (filtered = (engineers | filter: query | filter: searchKeyword | orderBy: orderByField:reverseSort))">
Big thanks to everyone who contributed to help me with this. G

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.

How to store REST api result in service to avoid requerying

service
app.factory('Notes', ['$resource', function($resource) {
return $resource('/notes/:id', {id: '#id'},
{
'query': {method: 'GET', params: { format: 'json'}, isArray: true},
'update': {method:'PUT'},
'save': {method:'POST'},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'}
},
{ stripTrailingSlashes: false}
);
}]);
Controller:
app.controller('NotesCtrl', ['$scope', '$routeParams', 'Notes',
function($scope, $routeParams, Notes) {
$scope.notes = Notes.query();
Notes.query(
function(data) {
$scope.notes = data;
// success handler
},
function(error) {
alert('error')
// error handler
}
);
}]);
If we are using Notes service in multiple controller it will fetch data again and again.
How to avoid fetching data? I want to fetch data only once and then serve same data to every controller.

angularjs factory with $resource doesn't work

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 ?

How to use success and error in Angularjs if not using $http?

Below is my code for querying a resultset in angularjs. Below is both controller.js and services.js. However I want to get .success and .error defined in my getResult call so that I can defined different $scopes for each flow and show it on UI accordingly. I searched but everywhere I got it for $http which I am not using. As I am new to angularjs, could you please help me out with it?
app.controller('DemoCtrl3', ['$scope', 'PFactory', '$location', function ($scope, PFactory, $location) {
$scope.getResult = function () {
$scope.allposts = PFactory.postmain.query();
$location.path('/view2');
}
services.js is:
return {
postmain: $resource('/ngdemo/web/posts', {}, {
query: {method: 'GET', isArray: true },
create: {method: 'POST'}
}),
You can pass success and error callbacks to resource actions (query, create, etc.) and get response data as callback parameter. Here is an example of doing it:
HTML
<body ng-controller="ctrl">
<h1>{{message}}</h1>
</body>
JavaScript
angular.module('app',['ngResource']).
service('PFactory', ['$resource', function($resource) {
return {
postmain: $resource('data.json', {}, {
query: {method: 'GET', isArray: true },
create: {method: 'POST'}
})
}
}]).
controller('ctrl', ['$scope', 'PFactory', function($scope, PFactory) {
PFactory.postmain.query(function success(data){
$scope.message = 'Number of records loaded: '+data.length;
}, function error() {
$scope.message = 'Server Error!'
});
}]);
Plunker: http://plnkr.co/edit/k5LgMPkU6jAteaFCn74C?p=preview
AngularJS documentation: $resource

Resources