-5

I have a json object s.BdayDetails and i want to change the values of my s.BdayDetails.ProvID into another value.



 for(var i=0;i<s.BdayDetails.length;i++){

h.post("../Event/getProvinceName?ProvID=" + s.BdayDetails[i].ProvID).then(function (r) {

s.BdayDetails[i].ProvID = r.data.toString(); })

Returns an error Cannot set property 'ProvID' of undefined. But when I console.log(r.data.toString()) it shows the values.

Keen
  • 5
  • 3

2 Answers2

1

You can try something like the below code to achieve the same as you described. Please check with this plunker link for the example code implementation.

Controller:

  var s ={
    BdayDetails: [{
      ProvID: '001',
      ProvName: 'KARNATAKA'
    },{
      ProvID: '002',
      ProvName: 'GOA'
    }]
  };
  var indx = 0;

  function nextProvince(){
    if(indx < s.BdayDetails.length){
      $http.post("../Event/getProvinceName?ProvID=" + s.BdayDetails[indx].ProvID).then(function (r) {
          s.BdayDetails[indx].ProvID = r.data.toString();
          indx++;
          nextProvince();
      });
    }else{
      indx=0;
    }
  }
  nextProvince();
Immanuel Kirubaharan
  • 1,094
  • 1
  • 11
  • 17
0

Try This.

This is called closure inside the loop.

for(var i=0;i<s.BdayDetails.length;i++){
   function(index){
        h.post("../Event/getProvinceName?ProvID=" + s.BdayDetails[i].ProvID).then(function (r) {
            s.BdayDetails[index].ProvID = r.data.toString();
        })
   })(i);
 }
}
Kaustubh Khare
  • 3,280
  • 2
  • 32
  • 48