Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I’m new to angularjs (and programming). I’m trying to display a json file to my view (home.html) with $http and ngRepeat but it does not work. When I inspect angular responses, I see that there are tens of scopes. Each scope contains a character of my json file.

here is my controller (index.html)

    <!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My AngularJS App</title>
  <link rel="stylesheet" href="css/app.css"/>
  <link rel="stylesheet" href="css/bootstrap.css">
</head>
<body ng-app="monApp">

<div ng-view></div>

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
  <script src="https://code.angularjs.org/1.2.16/angular-route.js"></script>

  <script>
  var monApp = angular.module('monApp', []);
  monApp.config(function($routeProvider){
    $routeProvider
      .when('/', {templateUrl: 'partials/home.html', controller: 'StudentListController'})
      .when('/secteurs/:id', {templateUrl: 'partials/secteurs.html', controller: 'SecteursController'})
      .otherwise({
        template: "ca nexiste pas!"
      });
  });

  monApp.controller('StudentListController', function($scope, $http){

    $http.get('student.json').success(function(data){
      $scope.students = data;
    });

  });

  monApp.controller('SecteursController', function($scope, $routeParams){
    $scope.secteur = students[$routeParams.id];
  });
  </script>



  <!--In production use: <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>-->
  <!--<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>-->



  <script src="js/app.js"></script>
  <script src="js/services.js"></script>
  <script src="js/controllers.js"></script>
  <script src="js/filters.js"></script>
  <script src="js/directives.js"></script>
  <script src="css/jquery.js"></script>
  <script src="css/bootstrap.js"></script>
</body>
</html>

here is my view (home.html)

    <div class="container">
  <div class="row">
    <div class="col-md-4 col-md-4 col-md-4"></div>
    <div class="col-md-4 col-md-4 col-md-4">
        <div class="input-group input-group-lg col-lg-12">
            <input type="text" class="form-control" placeholder="search a student" ng-model="query"/>
        </div>
    </div>
</div>
  <div class="row" id="secteurmargin" >
    <div class="col-md-4 col-md-4 col-md-4"></div>
    <div class="col-md-4 col-md-4 col-md-4">

        <div ng-repeat="student in students | filter: query">
          <p>
            <a href="#/secteurs/{{student.id}}">{{student.name}}</a> 

          </p>
            <p>{{student.nickname}}</p>
        </div>


    </div>
  </div>

</div>

here is my json file

    [
            {"id": 0, "name": "Clément", "nickname": "lol"},
            {"id": 1, "name": "Léa", "nickname": "lulz"},
            {"id": 2, "name": "Romane", "nickname": "lolilol"}
];

Can you help me ? Thank you very much !

share|improve this question
    
The semicolon at the end of the JSON file will make it invalid. Angular is probably just treating it as a string and it's iterating it character by character. Remove it and see if it works. –  Anthony Chu Jun 30 at 20:54
    
Does console shows any errors ? –  sylwester Jun 30 at 21:19
    
Thank you very much ! It works ! –  Clemtek Jul 1 at 6:26
add comment

1 Answer 1

up vote 0 down vote accepted

You need to drop the semicolon per Anthony's comment and convert your serialized json string into a javascript object in your $http.get call:

 $scope.students = angular.fromJson(data);
share|improve this answer
    
Thank you very much, it works fine now ! –  Clemtek Jul 1 at 6:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.