I update the question now .
Mi view HTML
<a class="btn btn-success" ng-click="eliminartodo()">Eliminar todos</a>
Now I send my Controller Angular
.controller('ListCtrl', function($scope,$routeParams,Node,User) {
$scope.nodes = Node.query();
/**DeleteAll**/
$scope.eliminartodo = function () {
$scope.nodes = [];
};
})
And my Factory
nodeServices.factory('Node', ['$resource',
function($resource){
return $resource('api/nodes/:nodeId', {}, {
'get': {method:'GET', params:{nodeId:'#nodeId'}, isArray:false},
'save': {method:'POST'},
'query': {method:'GET', isArray:true},
'update': {method:'PUT'},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'}
});
}]);
In another code I use this and working properly
Node.remove({nodeId: nodeId}, $scope.node, function() {
$timeout(function() {
$location.path('/');
});
});
Related
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
user.json File has just one entry:
{
"id": 1,
"protocol": "http",
"ip": "255.255.255.0",
"port": "80",
"userName": "usemanager",
"password": "password",
"diskPath": "D:nwjs"
}
Factory Code:
duseApp.factory('duseConnect', function ($resource) {
return $resource("user.json");
});
Controller Code:
duseApp.controller("duseLoginController", function ($scope,duseConnect,
$location) {
var user = duseConnect.get({id: 1});
console.log(user);
console.log(user.id);
});
User Gets Listed.
But console.log(user.id); is undefined.
Why is it so?
OutPut of console.log(user);
output
It looks like your factory is returning promise and in that case you need to extract data from the promise.
duseConnect.get({id: 1}).then (function(user) {
console.log(user);
console.log(user.id);
});
The return of $resource is a resource "class" object with methods for the default set of resource actions optionally extended with custom actions. The default set contains these actions:
{ 'get': {method:'GET'},
'save': {method:'POST'},
'query': {method:'GET', isArray:true},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'}
};
The way of using it could be:
var User = $resource('/user/:userId', {userId:'#id'});
var user = User.get({userId:123}, function() {
user.abc = true;
user.$save();
})
In your case you can do something like:
duseApp.controller("duseLoginController", function ($scope,duseConnect,
$location) {
var user = duseConnect.get({id: 1}, function (response) {);
console.log(response);
console.log(response.id);
}});
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});
}
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.
I have used 'service' method to call a post method.
userServices.users(Restangular).post(data);
where
userServices is my factory function and
users is a function inside it.
app.factory('userServices', function ($http,$rootScope,cookie) {
return{
users: function(Restangular)
{
return Restangular.service('api/authenticate');
}
}
});
You can do this way.
app.factory('Test', function ($http) {
function getData(data, callback) {
var url = "URL";
$http.get(url).success(function (data) {
cachedData = data.results;
callback(data.results);
});
}
return {
list: getData,
};
});
Just use $resource
app.service('ObjectService', function($resource) {
var _service = {};
_service.Data = function(path) {
return $resource(path, null,
{
'create': {method:'POST'},
'get': {method:'GET'},
'query': {method:'GET', isArray:true},
'update': { method:'PUT' },
'remove': {method:'DELETE'}
}
);
};
return _service;
});
and call that
var Test = ObjectService.Data('[end-point]/api/test');
Test.get(function(result) { //some manipulation });