jeudi 19 février 2015

Which things i can put in service in angular Js


I have read many articles but still could not find how to put stuff in services.


Currently this is my Service



angular
.module('users')
.factory('objectiveService', objectiveService);

objectiveService.$inject = ['$http', 'Restangular'];

function objectiveService($http, Restangular) {
return {
getObjectives: getObjectives,
getSingleObjective: getSingleObjective
};

function getObjectives(pid) {
var pr = Restangular
.all('api')
.all('users')
.one('subjects', pid)
.all('objectives');

return pr;
}

function getSingleObjective(oid) {
var pr = Restangular
.all('api')
.all('users')
.one('objectives', oid);

return pr

}
}


This is the controller:



var _vm = this;
this.objPromise = objectiveService.getObjectives(44);

function getData() {
var promise = _vm.objPromise;

promise
.getList(filters)
.then(function(result) {
$scope.gridData = result;
});
}

function remove(id) {
if (confirm('Are you sure you want to delete this!')) {
objectiveService.getSingleObjective(id).remove().then(function() {
$scope.getData();
});

}
}

// initial call
$scope.getData();


In this code i basically see no use to define service because I still has to use then() in the controller to assign data to Grid.


Also I can't use then() in service because there I don't have $scope to update the data.


People say to do all stuff in service but I am not able to figure out how.


Ideally I want to put all functions like remove(object_id) in service


IS it possible that I can just do objectiveService.remove(id) in controller.


But then I have to call $scope.getData() after deleting which I can't do in service





Aucun commentaire:

Enregistrer un commentaire