AngularJS $ http.get为ng-data-repeat指令检索不可用格式的数组

时间:2016-10-30 03:05:49

标签: javascript angularjs arrays

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

[{"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指令?任何小帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

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

你应该这样做:

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

&#13;
&#13;
var app = angular.module("QuizApp", []);

app.controller("QuizCntrl", function ($scope, $http) {
  $scope.Quizes = [{"quizname":"Quiz 1", "username":"musa"}, {quizname:"Quiz 2", username:"musa 2"}];
});
&#13;
<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>
&#13;
&#13;
&#13;