0

Trying to retrieve using angular the json found at: "https://api.github.com/users/angular", more precisely the id property from that json. Any thoughts on why this fails (in debug mode I get a

'response is not defined'

):

<html ng-app="myViewer">
<head>
<script data-require="angular.js@*" data-semver="1.2.25" src="https://code.angularjs.org/1.2.25/angular.js"></script>
</head>


<body ng-controller="MyController">
    {{ result.id }}
    {{ err }}
</body>

<script type="text/javascript">
(function() {

    var app = angular.module("myViewer", []);

    var MyController = function($scope, $http)
    {
        $http.get("https://api.github.com/users/angular")
        .then(function1, onerr);

        var function1 = function(response)
        {
            $scope.result = response.data;
        }
        var onerr = function(reason)
        {
            $scope.err = "nightmare";
        }

    }

    app.controller("MyController", ["$scope", "$http", MyController]);
}());

</script>
</html>

I expect the result to be the id that you can see by copying that link in your browser.

2
  • You're naming it MyController, but you have the var as SecondController, incongruity there. Commented Dec 5, 2017 at 15:26
  • fixed that was an error when i tried to simplify my code and namings for stackoverflow thank you Commented Dec 5, 2017 at 15:47

5 Answers 5

2

Here is a working fiddler https://jsfiddle.net/paka2/8roj37q9/1/

angular.module("myViewer", []).controller("myCtrl", function($scope,$http){
this.result = {};
this.err ;  
         $http.get("https://api.github.com/users/angular")
        .then(function1, onerr) ;
    function function1 (response)
    {    
      $scope.result = response.data;
    }
    function onerr (reason)
    {
      $scope.err = "nightmare";
    }
});

Hope it helps!!

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

Comments

1

Works if I correct the order:

var function1 = function(response)
{
    $scope.result = response.data;
}
var onerr = function(reason)
{
    $scope.err = "nightmare";
}

$http.get("https://api.github.com/users/angular")
.then(function1, onerr);

function1 should be defined before i call it with then.

1 Comment

Yes. Function expressions are hoisted :)
0

I think your promise is wrong.

  $http.get('https://api.github.com/users/angular').then(function(response){
    $scope.result = response.data;
  })

See same HTTP call working below:

https://plnkr.co/edit/8JqZ5NukHnHWGPxZ0GRr

Comments

0

try this i think you have incorrectly defined the controller as mycontroller and it should be SecondController instead.

 app.controller("MyController", ["$scope", "$http", SecondController ]);

1 Comment

fixed that was an error when i tried to simplify my code and namings for stackoverflow thank you
0

Try with following

var function1 = function(response) { $scope.result = response; }

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.