1

hi all i am using angularjs http get method i get the values dynamically based on my loop but i am not able to get the value from http get help how to do this and solve the problem

for(i=0;i<4;i++){
    var stream =i;
    $http.get('/ViewGetBUMaterialStream/' + stream  ).then(function (response) {
                   $scope.ViewStreams[i] = response.data;
    });
}
4
  • 1
    You should probably post a bit more, oh also if you actually use + Stream that variable is not defined here, why not use the i there too Commented May 10, 2017 at 5:21
  • Problem is, by the time all your HTTP requests finish, i is and will only be the value 4 Commented May 10, 2017 at 5:31
  • Possible duplicate of JavaScript closure inside loops – simple practical example Commented May 10, 2017 at 5:32
  • you need to learn javascript at first, instead of start from angular... Commented May 10, 2017 at 5:48

3 Answers 3

2

You can use let keyword. This is a usually problem for closures.

The let statement declares a block scope local variable, optionally initializing it to a value.

for(let i=0;i<4;i++){
    var stream =i;
    $http.get('/ViewGetBUMaterialStream/' + stream  ).then(function (response) {
               $scope.ViewStreams[i] = response.data;
    });
}

Another method is to use a Immediately-invoked function expression.

(function(i){
  $http.get('/ViewGetBUMaterialStream/' + Stream  ).then(function (response) 
     {
        $scope.ViewStreams[i] = response.data;
     });
})(i);
Sign up to request clarification or add additional context in comments.

2 Comments

How the let keyword will fix the issue. And to use let he need es6.
let keyword declares a block scope local variable, optionally initializing it to a value and that's you needed.
1

This issue is caused because of javascript. Unfortunately, javascript does not have a scope for a loop, variables declared in a for loop are in a function scope. You should use an anonymous inner function and pass the value of i to that function block.

for(i=0;i<4;i++){
    var stream =i;
    (function(i){
      $http.get('/ViewGetBUMaterialStream/' + Stream  ).then(function (response) {
                   $scope.ViewStreams[i] = response.data;
      });
    })(i);
}

4 Comments

"javascript does not have a scope for a loop" It does.
@zeroflagL Confusion should be because of my poor choice of words. Javascript does not have a separate scope for loops. Am I missing something?
"Am I missing something?" Yes. JavaScript does have a separate scope for loops (iteration scope). But it requires the usage of let.
@zeroflagL Sorry man. I assumed that we are still in es5 era.
1

try this to bind i to your callbacks.

for(i=0;i<4;i++){
  var stream =i;
  $http.get('/ViewGetBUMaterialStream/' + stream  ).then(function (response) {
    $scope.ViewStreams[i] = response.data;
  }.bind(i));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.