I cannot able to use data retrive by $http via factory


#1

i cannot able to use data retrive by $http via factory function althought .their is an intreasting part of it data display on console loud and clear but not on the ng-repeat

var app =angular.module('todo.store', []);
            app.factory('TodoStore',function(){
               var notes=[];
                return{
                    list:function($http){
                        $http({
    			        method  : 'GET',
    			        url     : 'http://testy.6te.net/getTask.php?',
                                    params :{id:1}
    			         
    			    }).then(function(data) {
    			           notes  = data.data;
                                       console.log(notes);
                                   });
                                   return notes;
                    },

#2

Please read this article and update your question.


#3

u mean i have to google it first :slight_smile: i did but they are not close to my case


#4
  • You’re not been able to retrieve data how?
  • What’s happening?
  • At what point?
  • Have you received any error?
  • If yes what’s it saying?
  • Is your server receiving REST call?

All of these is important.


#5

my notes does not show on views

part of controller file

    app.controller('MainCtrl',function($scope,$http,TodoStore){
       $scope.todos =TodoStore.list($http);
    });

part of view file
    <ion-view view-title="todos" ng-controller="MainCtrl">
        <ion-content >
              <ion-list show-reorder="reordering">
                  <ion-item  href="#/edit/{{x.id}}" ng-repeat="x in todos track by x.id">
                      <h2>{{x.title}}</h2>
                      <p>{{x.details}}</p>
                      <ion-reorder-button class="icon ion-drag" on-reorder="move(x,$fromIndex,$toIndex)"></ion-reorder-button>
                      <ion-option-button class="button-assertive" ng-click="del(x.id)">Delete</ion-option-button>
                  </ion-item>
              </ion-list>
        
      </ion-content>

#8

First, I think you must inject the $http injection in your factory.

If you want use the data in the factory, you should use success() callback instead then() callback.

If you want to use it in your controller, you can create a defer using $q.defer(). Then, In the success callback set the response to the resolve and return the promise to the controller

var app =angular.module('todo.store', []);
        app.factory('TodoStore',function($http, $q){
           var notes=[];
            return{
                list:function(){
                    var deferred = $q.defer();
                    $http({
			        method  : 'GET',
			        url     : 'http://testy.6te.net/getTask.php?',
                                params :{id:1}
			         
			    }).success(function (response){
                                   // This is the data from your request
                                   deferred.resolve(response)
                            }).error(function () {
                                  // If something was wrong
                                  deferred.reject();
                            });
                       return deferred.promise;
                };

In your controller

app.controller('MainCtrl',function($scope,TodoStore){
   TodoStore.list().then(function (response) {
           // Here will be the data after the request finished
   }, function (err) {
          // Here will be if there was an error
   })
});

Hope it helps


#9

i had injected but not worked it might be a problem of the variable ‘notes’ scope how can i ref that notes on succes to append the data?


#10

I updated my previous answer. Maybe it helps you


#11

it workes but i donot know what are promesses and how it uses please ref me tutorial other then angular docs as i am newbie


#12

@owndmorix how can i append the notes variable by the data i get?


#13

Check this tutorials


http://chariotsolutions.com/blog/post/angularjs-corner-using-promises-q-handle-asynchronous-calls/
http://markdalgleish.com/2013/06/using-promises-in-angularjs-views/
https://thinkster.io/a-better-way-to-learn-angularjs/promises

The $http request are asyncronous, for that reason, if you do TodoStore.notes, maybe it doesn’t have information at the time you call it.

By this way, you ensure to get the information at the time the $http has finished


#14
  i don not know y it does not adding  in notes

news:function(x){notes=x;
            console.log(notes);
            return notes;
        }

Controller

TodoStore.list().then(function (response) {
        $scope.todos=response;
         TodoStore.news($scope.todos);
   });

#15

If you want to add it locally. You can updated your get method to this:

return{
list:function(){
var deferred = $q.defer();
$http({
method : ‘GET’,
url : 'http://testy.6te.net/getTask.php?’,
params :{id:1}

		    }).success(function (response){
                                setNotes(response);
                               // This is the data from your request
                               deferred.resolve(response)
                        }).error(function () {
                              // If something was wrong
                              deferred.reject();
                        });
                   return deferred.promise;
            },
            setNotes: function (nots) {
                  notes = nots
            },
            addNote: function (note) {
                  notes.push(note)
            },
           allNotes: function () {
                return notes;
           }

#16

nope that is not working for me it seems that i have to use promises again and again for all CRUD operations


#17

You can use this way too


#18

hi it works perfectly well but the view shows previous not the lateast it donot reload i have to reload it manually to get the result is there any way to update reload on edit ?


#19

Yes sure. You can wrap your code in a $timeout() where you do the update the $scope variable to notice the view that was an update


#20

In controller u need to add factory service name and call the function which ic used to data. And u need to add dependenct injuction in serve. works fine for me