0

This is the code I am using. currentId is 10; I am making a service call which has a function of $http.get(to a JSON) and I want to attack the array in JSON's length to currentId after the function is executed. How do I do it? Is there any special function in angularjs which helps me. I have read the other relevant questions here, but I need this done in angularjs. Thanks.

var currentId = 10;
    console.log(currentId + ' before function'); //outputs 10


    function findId(){
        readJson.readJsonfun().then(function(data) {
            currentId = data.length; //say data.length = 20;
            return currentId;
    });}

    findId();

    console.log(currentId + ' before function');  //should output 20?
Aijaz
  • 680
  • 15
  • 42
  • 1
    Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Austin Schmidt Jul 26 '16 at 21:16

2 Answers2

0

As it's Async function - use async approach

var currentId = 10;
var modifiedId1;
var modifiedId2;

console.log(currentId + ' before function call');


function findId(){
    return readJson.readJsonfun().then(function(data) {
        currentId = data.length;
        return $q.when(currentId);
    });
}

findId().then(function(id){
    //first time we need it
    firstFunction(currentId); //or firstFunction(id);
});

function firstFunction(id){
   modifiedId1 = id * 2;
}

function secondFunction(){
   return findId().then(function(id){
       //second time we need it, currentId is updates
       modifiedId2 = id * 4;
       return $q.when();
   });
}
uamanager
  • 1,128
  • 8
  • 11
  • Thanks! The thing is I cannot use currentId afterwards, I need to use it later as well, and after the .then function it goes back to the same value. How do I change it permanently? – Aijaz Jul 26 '16 at 23:07
  • did u test my solution? if u need to do some stuff after finId function execution - add this stiff instead of console.log – uamanager Jul 26 '16 at 23:15
  • I did. And it does work, but I have a line of functions running later on, and I need to call findId() several times, hence I need to use the new variable later on, and I cannot replace the console log message instead of allt he functions. Is there anyway to handle this?And thanks again. – Aijaz Jul 26 '16 at 23:33
  • @Aijaz If u start using async approach u need to use it everywhere, so every time u need to access currentId variable u need to wrap actions in promise handler, i'll update answer to show u how it can be – uamanager Jul 26 '16 at 23:39
0

Store the variable of concern (currentID) in the service. Inside your service, it would look something like this:

.service('Example', function(){

  var service = this;
  var currentID = null;

  service.getCurrentID = function() {
    return currentID;
  }

  service.findId(){
      readJson.readJsonfun().then(function(data) {
          currentId = data.length; //say data.length = 20;
  });}

Then, from outside of the service, you could call Example.findID() to update the currentID, and then Example.getCurrentID() to retrieve the actual value

eazy_g
  • 377
  • 3
  • 10