0

I've just started learning angularJS and am trying to figure out why the following code doesn't work

<html>
    <head>
        <title>Angular JS Ajax</title>
    </head>

    <body>
        <div ng-app="mainApp" ng-controller="studentController">
            <table>
                <tr>
                    <th>Name</th>

                    <th>Roll No</th>

                    <th>Percentage</th>
                </tr>

                <tr ng-repeat="student in students">
                    <td>{{ student.Name }}</td>

                    <td>{{student.RollNo}}</td>

                    <td>{{student.Percentage}}</td>
                </tr>
            </table>
        </div>

        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>

        <script>
            var mainApp = angular.module("mainApp");

            mainApp.controller("studentController", function($scope,$http){

                var url = "data.txt";

                $http.get(url).success(function(response){
                    $scope.students = response;
                });
            });
        </script>
    </body>
</html>

When I change the attribute of ng-app to "" (instead of "mainApp"'), and replace the` code above with the following, the application works. I'd really appreciate if anyone could explain to me why this is the case.

    <script>
        function studentController($scope, $http){
            var url = "data.txt";

            $http.get(url).success(function(response){
                $scope.students = response;
            });
        };
    </script>

Here's the data.txt file:

[
    {
        "Name": "Mahesh Parashar",
        "RollNo": 101,
        "Percentage": "80%"
    },

    {
        "Name": "Dinkar Kad",
        "RollNo": 191,
        "Percentage": "75%"
    }
]

Thank you!

1
  • what error you see in console? Commented Feb 18, 2016 at 3:07

3 Answers 3

1

You forgot to add [] with app module declaration

var mainApp = angular.module("mainApp",[]);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Paresh, Do you mind explaining to me the point of having the [] brackets, and whether they ever contain anything or should be empty in all cases? Thank you!
No it not empty always, you need to add depencies like ngMap, ngSenitize etc
1

The first one is not working because you don't supply the right syntax for mainApp. You need to add [] for module declaration. This should look like below:

var mainApp = angular.module('mainApp', []);
    mainApp.controller('studentController', function($scope, $timeout) {
      var url = "data.txt";

      $http.get(url).success(function(response) {
          $scope.students = response;
      });
 });

The second one is working fine simply because you don't have any app module declaration.

2 Comments

Ahhh, I complete forgot about the []. Do you mind explaining why we need the [] in the syntax, and do these brackets ever contain anything in some cases? Thanks!
The bracket means injecting dependencies like 'ngAnimate', 'ngAria', 'ngCookies', 'ngResource', and etc depends on your project.
0

You need to Parse it to json object Please try JSON.parse.

jsonObj = JSON.parse(response);

Hope this will help you

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.