0

I am new with Angular. I download JSON and then must handle it. I wrote something like a function:

for (var i = 0; i < $scope.Objects.length; i++){

    $http.get($scope.Objects[i]["Commit"]).success(function(data) {
        Commit = data;
    })

        .error(function(data, status, headers, config) {
            alert("Sorry we have some troubles... Try another time");});



        for (var j = 0; j < Commit.length; j++){
            //Metrics
            if (j = 0){
                for (var k = 0; k < Commit[j].length(); j++){
                    if (Commit[j][k] < 40){
                        $scope.Objects[i]["State"] = "Rejected"
                    }
                }
            }
            ///////////////////////////////////////////////////////

            //Build
            if (j = 1){
                for (k = 0; k < Commit[j].length(); j++){
                    if (Commit[j][k] == false){
                        $scope.Objects[i]["State"] = "Rejected"
                    }
                }
            }
            ///////////////////////////////////////////////////////


            //UnitTests

            if (j = 2){
                if (Commit[j][0] > 15){
                    $scope.Objects[i]["State"] = "Rejected"
                }
                for (k = 1; k < Commit[j].length(); j++){
                    if (Commit[j][k] < 40){
                        $scope.Objects[i]["State"] = "Rejected"
                    }
                }
            }
            ///////////////////////////////////////////////////////


            //FuncTests

            if (j = 3){
                if (Commit[j][0] > 2500){
                    $scope.Objects[i]["State"] = "Rejected"
                }
                for (k = 1; k < Commit[j].length(); j++){
                    if (Commit[j][k] < 40){
                        $scope.Objects[i]["State"] = "Rejected"
                    }
                }
            }
            /////////////////////////////////////////////////////////

        }
    $scope.Objects[i]["Commit"] = Commit;


}`

but I can't handle it after $http because it isn't a $scope or even a function, I want use this piece of code how it is, like in pure JS. Is it possible.

14
  • I know that there is something like .service, but I don't know how to use it Commented May 9, 2015 at 18:00
  • Did you inject $http service in your controller ? Commented May 9, 2015 at 18:08
  • yes, i did, the $scope functions work well but this one can't understand $scope.Object Commented May 9, 2015 at 18:16
  • it's hard to guess then, can you provide complete example using say plnkr.co ? Commented May 9, 2015 at 18:18
  • any code sharing platform like - plnkr.co or jsfiddle.net Commented May 9, 2015 at 18:21

1 Answer 1

1

First received data and only afterwards do operations with it. Otherwise it will be undefined. One of the ways it can be done like this, check this link: https://jsfiddle.net/oxuh7h85/22/

 $http.get('https://demo6732873.mockable.io/Users').success(function (data) {
            //MyData = data;
            $scope.Objects = data;
               console.log('data received - 3 ', data);
            $scope.runActionwithObjects();

        })
Sign up to request clarification or add additional context in comments.

1 Comment

cheers! check if that works in your exact setup. if yes - accept the answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.