简体   繁体   English

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

[英]Angular Js ng-repeat can't getting output from json array using ajax

I am using Angular Js having ajax but by using ng-repeat can't getting output from json array.我正在使用具有 ajax 的 Angular Js,但通过使用 ng-repeat 无法从 json 数组中获取输出。 On alert getting [Object object]...so on but by using $scope.names= response[0].id;在警报获取 [Object object]...等但使用$scope.names= response[0].id; , getting "1" id on alert. ,在警报中获得“1”ID。 I need to get all data in table.我需要获取表中的所有数据。 Is there any way to get all data in table and also I am not using $http in controller because it not working in mobile browser but works in desktop, i don't know why?Please help!有什么方法可以获取表中的所有数据,而且我没有在控制器中使用$http因为它不能在移动浏览器中工作但可以在桌面上工作,我不知道为什么?请帮忙!

JS 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 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@gmail.com"},{"id":"2","name":"pratyush","email":"p@gmail.com"},{"id":"3","name":"ankit","email":"a@gmail.com"},{"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"}]

Instead of ajax call , use $http with angular as below而不是 ajax call ,使用$http和 angular 如下

var app = angular.module('studentApp', []);
app.controller('StudentCntrl', function($scope,$http) {
    $http.get('data.json').then(function (response){
                  console.log(response.data.pages);
                $scope.names = response.data;
        });
});

Here is the working DEMO这是工作演示

When updating the scope with an $ajax call (you should use $http), you have to use $scope apply to update your bindings.当使用 $ajax 调用更新作用域时(您应该使用 $http),您必须使用 $scope apply 来更新您的绑定。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM