在angularjs中使用$ http向视图显示json文件

克莱姆泰克

我是angularjs(和编程)的新手。我正在尝试使用$ http和ngRepeat在我的视图(home.html)中显示一个json文件,但是它不起作用。当我检查角度响应时,我发现有几十个范围。每个范围都包含我的json文件的一个字符。

这是我的控制器(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>

这是我的观点(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>

这是我的json文件

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

你能帮助我吗 ?非常感谢你 !

emp

您需要根据Anthony的注释删除分号,然后在$ http.get调用中将序列化的json字符串转换为javascript对象:

 $scope.students = angular.fromJson(data);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在angularjs中使用$ http向视图显示json文件

来自分类Dev

在AngularJS服务中使用$ http

来自分类Dev

在AngularJS + WebAPI中使用$ http

来自分类Dev

在AngularJS服务中使用$ http回调

来自分类Dev

在http发布AngularJS异步中使用循环

来自分类Dev

在AngularJS中使用重复的API JSON响应

来自分类Dev

如何在使用angularjs的路由中使用多视图

来自分类Dev

在$ http回调中使用AngularJS和Typescript使用'this'

来自分类Dev

使用PhoneGap下载文件并在AngularJS中使用

来自分类Dev

使用AngularJS在键名中使用空格解析JSON

来自分类Dev

使用ng-options在AngularJS中使用JSON填充select

来自分类Dev

在angularjs的视图,服务,控制器中使用的配置常量

来自分类Dev

要在视图中使用的值中的响应数据(AngularJS)

来自分类Dev

在 AngularJS 中使用嵌套视图时如何保留引用?

来自分类Dev

在 angularjs 中使用 ng-view 替换视图

来自分类Dev

在ASP.NET MVC中使用AngularJS显示ModelState错误

来自分类Dev

如何在HTML中使用AngularJS成对显示图像

来自分类Dev

在angularjs-rails-resource中使用http缓存

来自分类Dev

Angularjs; 在服务中使用$ http返回引用而不是实际数据

来自分类Dev

AngularJS:如何在过滤器中使用$ http

来自分类Dev

在AngularJS中使用$ http服务时的可变范围

来自分类Dev

如何在AngularJS中使用重试逻辑重用HTTP请求

来自分类Dev

在AngularJS中使用$ resource和$ http管理全局错误

来自分类Dev

Angularjs; 在服务中使用$ http返回引用而不是实际数据

来自分类Dev

在angularjs-rails-resource中使用http缓存

来自分类Dev

在AngularJS中使用$ http服务发布多个数组

来自分类Dev

如何在AngularJS中使用Jasmine测试$ http

来自分类Dev

在 AngularJS 的 .service 中使用 $http 未定义“get”

来自分类Dev

如何在MVC中使用AngularJS上传文件

Related 相关文章

  1. 1

    在angularjs中使用$ http向视图显示json文件

  2. 2

    在AngularJS服务中使用$ http

  3. 3

    在AngularJS + WebAPI中使用$ http

  4. 4

    在AngularJS服务中使用$ http回调

  5. 5

    在http发布AngularJS异步中使用循环

  6. 6

    在AngularJS中使用重复的API JSON响应

  7. 7

    如何在使用angularjs的路由中使用多视图

  8. 8

    在$ http回调中使用AngularJS和Typescript使用'this'

  9. 9

    使用PhoneGap下载文件并在AngularJS中使用

  10. 10

    使用AngularJS在键名中使用空格解析JSON

  11. 11

    使用ng-options在AngularJS中使用JSON填充select

  12. 12

    在angularjs的视图,服务,控制器中使用的配置常量

  13. 13

    要在视图中使用的值中的响应数据(AngularJS)

  14. 14

    在 AngularJS 中使用嵌套视图时如何保留引用?

  15. 15

    在 angularjs 中使用 ng-view 替换视图

  16. 16

    在ASP.NET MVC中使用AngularJS显示ModelState错误

  17. 17

    如何在HTML中使用AngularJS成对显示图像

  18. 18

    在angularjs-rails-resource中使用http缓存

  19. 19

    Angularjs; 在服务中使用$ http返回引用而不是实际数据

  20. 20

    AngularJS:如何在过滤器中使用$ http

  21. 21

    在AngularJS中使用$ http服务时的可变范围

  22. 22

    如何在AngularJS中使用重试逻辑重用HTTP请求

  23. 23

    在AngularJS中使用$ resource和$ http管理全局错误

  24. 24

    Angularjs; 在服务中使用$ http返回引用而不是实际数据

  25. 25

    在angularjs-rails-resource中使用http缓存

  26. 26

    在AngularJS中使用$ http服务发布多个数组

  27. 27

    如何在AngularJS中使用Jasmine测试$ http

  28. 28

    在 AngularJS 的 .service 中使用 $http 未定义“get”

  29. 29

    如何在MVC中使用AngularJS上传文件

热门标签

归档