javascript - Angular Js ng-repeat无法使用ajax从json数组获取输出

标签 javascript jquery angularjs json ajax

我正在使用具有 ajax 的 Angular Js,但使用 ng-repeat 无法从 json 数组获取输出。警报获取 [Object object]...等等,但通过使用 $scope.names= response[0].id; ,在警报时获取“1”id。我需要获取表中的所有数据。有什么方法可以获取表中的所有数据,而且我没有在 Controller 中使用 $http 因为它不能在移动浏览器中工作,但可以在桌面上工作,我不知道为什么?请帮忙!

JS

var app = angular.module('studentApp',[]);
app.controller('StudentCntrl', function($scope){

$.ajax({
    url : '/fetchAllData',
    type : 'GET',
    success : function(response){
        $scope.names=response;
        //alert($scope.names);(This is working)
          } 
     });
});

JSP

<div ng-app="studentApp" ng-controller="StudentCntrl">

      <div class="table-responsive">
        <table class="table table-striped">
          <thead>
            <tr>
              <th>ID</th>
              <th>NAME</th>
              <th>EMAIL</th>
            </tr>
          </thead>
          <tbody>
            <tr ng-repeat="x in names">
              <td>{{x.id}}</td>
              <td>{{x.name}}</td>
              <td>{{x.email}}</td>

            </tr>
          </tbody>
        </table>
      </div>

Json

[{"id":"1","name":"abdul","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6a786a1aba7afaae8a5a9ab" rel="noreferrer noopener nofollow">[email protected]</a>"},{"id":"2","name":"pratyush","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e7e4e69636f6762206d6163" rel="noreferrer noopener nofollow">[email protected]</a>"},{"id":"3","name":"ankit","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="365776515b575f5a1855595b" rel="noreferrer noopener nofollow">[email protected]</a>"},{"id":"45","name":"kjhj","email":"kjhkj"},{"id":null,"name":null,"email":null},{"id":null,"name":null,"email":null},{"id":null,"name":null,"email":null},{"id":"trffs","name":null,"email":null},{"id":"afa","name":"sdgfdsg","email":"dsagdsg"},{"id":"12","name":"pppp","email":"hjk,gh"}]

最佳答案

当使用 $ajax 调用更新范围时(您应该使用 $http),您必须使用 $scope apply 来更新您的绑定(bind)。

$scope.$apply(function(){
    $scope.names = response.data;
});

关于javascript - Angular Js ng-repeat无法使用ajax从json数组获取输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39549876/

相关文章:

javascript - innerHTML 在 JSFiddle 测试项目中不起作用

javascript - AngularJS Json 编辑器

javascript - 如何将结果添加到我的范围 ng-click?

javascript - 将 div 实例化到容器内的随机空间

javascript - 列表的 HTML5 session 存储

javascript - 使用 forEach 修改 Angular 范围变量

javascript - 在每个固定高度段落的最后一行添加省略号,并 overflow hidden -y

jQuery UI 可拖动和页面滚动(在移动设备上)

javascript - 如何将元素高度附加为其他元素样式值

angularjs - 你如何使 $mdDialog.prompt() 的字段成为必需的