0

In the below code,

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Dashboard</title>
        <!-- Libs -->
        <script src="angular.js"></script>
        <script>
        var countryApp = angular.module('countryApp', []);
        countryApp.controller('CountryCtrl', function ($scope, $http){
            $http.get('country_codes.json').success(function(data) {
                $scope.countries = data;
            });
        });
        </script>   
    </head>
    <body ng-controller="CountryCtrl">

    </body>
</html>

/* country_codes.json */

[
  {
    "code": "AD",
    "name": "Andorra",
    "population": "84000"
  },
  {
    "code": "AE",
    "name": "United Arab Emirates",
    "population": "4975593"
  }
]

As per debugging, I do not see any error and the callback functions does not execute.

Why the controller callback does not execute?

1
  • You should handle the .error callback too, not just the .success. Commented Jan 6, 2016 at 18:09

2 Answers 2

2

After adding ng-app="countryApp" in your html tag its works for me.

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

2 Comments

Yes, ng-app tells angularjs that HTML tag element is the "owner" of an AngularJS application. You can add ng-app to any tag like a div tag
But still the code does not work. Can you fiddle the code?
1

Add error function, and you will see error details:

<script>
        var countryApp = angular.module('countryApp', []);
        countryApp.controller('CountryCtrl', function ($scope, $http){
              $http.get('country_codes.json')
    .then(function(data) {$scope.countries = data;}
,
          function(data) {console.log("error: "+data)});
 });
 </script>  

change the json to:

{
    "countries": [
     {
    "code": "AD",
    "name": "Andorra",
    "population": "84000"
  },
     {
    "code": "AE",
    "name": "United Arab Emirates",
    "population": "4975593"
  }

    ]  

}

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.