doe
doe

Reputation: 148

How to call a then method after a success method in Angular JS

I have two functions. After one completes I want to use the then method to call the next. I can't get the structure correct.

success method

 self.Read_Data = function () {
    return Ajax.Get({
        url: READ_DATA,
        success: function (data) 
        {
            self.Orderdata = data.data;
        }
        , error: function (result, status, headers) {
        }
    });
}
self.Read_Data()

then method- another function

self.CopyOrderData = function () {
   self.CurrentData = [];
   for (var i = 0; i < self.Orderdata.length; i++)
   {
       self.CopyOrderData = { Checked: 0,OrderNumber: self.Orderdata[i].OrderNumber, OrderName: self.Orderdata[i].Name };
   };
   return 
}

How do I combine the CopyOrderData function to be a then method to Read_data()?

What I tried was this (but it didn't work):

self.Read_data().then=function(){
 self.CurrentData = [];
   for (var i = 0; i < self.Orderdata.length; i++)
   {
       self.CurrentData = { Checked: 0,OrderNumber: self.Orderdata[i].OrderNumber, OrderName: self.Orderdata[i].Name };
   };
   return 
 }
}

So basically I have a function which I load the data then I want to copy the data into a different array.

Upvotes: 1

Views: 241

Answers (2)

nalinc
nalinc

Reputation: 7425

Why not simply move the contents of CopyOrderData() within success handler ?

 self.Read_Data = function () {
    return Ajax.Get({
        url: READ_DATA,
        success: function (data) 
        {
            self.Orderdata = data.data;
            self.CurrentData = [];
               for (var i = 0; i < self.Orderdata.length; i++)
               {
                  self.CopyOrderData = { Checked: 0,
                  OrderNumber:self.Orderdata[i].OrderNumber, 
                  OrderName: self.Orderdata[i].Name };
               };    

        }
        , error: function (result, status, headers) {

        }

    });
}
self.Read_Data()

On a lighter note, its not the 'angular-way'of doing things. You should use $http service (or $resource) to make REST calls. So something like,

$http.get(READ_DATA).then(function(data){
   //do stuff with 'data'
});

is good to start with. Alternatively, you may also use separate success and error handlers.

self.Read_Data = function () {
   return $http.get(READ_DATA)
 }

and handle responses like

Read_Data().then(function(){
        //success_handler
     },function(err){
        //error_handler
      }
   );

or

Read_Data().success(function(){
        //success_handler
     }).error(function(err){
        //error_handler
      });

Cheers!

Upvotes: 2

Nikhil Batra
Nikhil Batra

Reputation: 3148

Try this

self.Read_data().then(function(){
 self.CurrentData = [];
   for (var i = 0; i < self.Orderdata.length; i++)
   {
       self.CurrentData = { Checked: 0,OrderNumber: self.Orderdata[i].OrderNumber, OrderName: self.Orderdata[i].Name };
  }
   return 

});

Upvotes: 2

Related Questions