rest - AngularJs Save data.d.results in var result = -


is possible save/replace data $ http ( data.d.results ) var result = ?

 .factory('contactservice', [function () {       var factory = {};        factory.getcontacts = function () {         return contactlist;       }        // contact list, separate database       var contactlist = [         {id: 0, name: 'ned stark', email: 'ned@winterfell.com', phone: '123-456-7890', url: 'www.google.com', notes: 'winter coming.'},          ];        return factory; 

example above works! want put data.d.results result in

var contactlist =

$http({  method: 'get',  url: 'url',   headers: { "accept": "application/json;odata=verbose" }  }).success(function (data, status, headers, config) {  $scope.inprogs = data.d.results;  }) .error(function (data, status, headers, config) {  }); 

then should add set method in service

add method

factory.setcontacts = function (contacts) {     contactlist = contacts; } 

and call in http success function this

$http({  method: 'get',  url: 'url',   headers: { "accept": "application/json;odata=verbose" }  }).success(function (data, status, headers, config) {  $scope.inprogs = data.d.results;  contactservice.setcontacts(data.d.results); //the call of setter  }) .error(function (data, status, headers, config) {  }); 

your service should be

.factory('contactservice', [function () {   var factory = {};    factory.getcontacts = function () {     return contactlist;   }    factory.setcontacts = function (contacts) {     contactlist = contacts;   }    // contact list, separate database   var contactlist = [     {id: 0, name: 'ned stark', email: 'ned@winterfell.com', phone: '123-456-7890', url: 'www.google.com', notes: 'winter coming.'},      ];    return factory; 

reminder: don't forget import service in controler


Comments