美元AngularJS http.Get为ng-data-repeat指令检索一个不可用格式的数组

AngularJS $http.get retrieves an array in unusable format for the ng-data-repeat directive

本文关键字:一个 数组 格式 Get http AngularJS ng-data-repeat 检索 指令 美元      更新时间:2023-09-26

我无法找到使用data-ng-repeat指令将数组元素绑定到包含下面脚本的html页面的方法。问题是quises看起来像这样:

[{"quizname":"Quiz 1", "username":"musa"...}]

<script> 
   var app = angular.module("QuizApp");
   app.controller("QuizCntrl",function($scope,$http){
   var onSuccess = function(data, status, headers, config){
   $scope.Quizes = data.getQuizesResult; //Returns an size5, checked it using console.log
  };
 var promise = $http.get("http://localhost:59503/Service1.svc/GetQuizes");
 promise.success(onSuccess);
 });
</script>

所以当我尝试这样做时:

 <div data-ng-controller="QuizCntrl">
     <table>
      ...
       <tr data-ng-repeat = "quiz in Quizes">
          <td>{{quiz.username}}</td>
          <td>{{quiz.quizname}}</td>
        <tr>
       ...
      </table>

没有显示。在我所做的教程和在线示例中,所有数组都是这样的:

 $scope.array = [{username: "value".....}]

我怎么能在我的字段名周围的引号工作,以使用data-ng-repeat指令?任何小小的帮助都将不胜感激。谢谢你。

正如您在示例中看到的,引号并不重要。也许你会在这个例子中发现你的错误。

你应该这样做:

$http.get("http://localhost:59503/Service1.svc/GetQuizes").then(onSuccess);

var app = angular.module("QuizApp", []);
app.controller("QuizCntrl", function ($scope, $http) {
  $scope.Quizes = [{"quizname":"Quiz 1", "username":"musa"}, {quizname:"Quiz 2", username:"musa 2"}];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="QuizApp">
  <div data-ng-controller="QuizCntrl">
     <table>
       <tr data-ng-repeat = "quiz in Quizes">
          <td>{{quiz.username}}</td>
          <td>{{quiz.quizname}}</td>
        <tr>
      </table>
  </div>
</div>