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

标签 javascript angularjs arrays

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

[{"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>

关于javascript - AngularJS $http.get 检索 ng-data-repeat 指令不可用格式的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40325692/

相关文章:

javascript - Angular JS : Directive to change all links within a block of html

javascript - Angular 数据绑定(bind) - 手动设置模型值

javascript - JavaScript 中将对象从对象数组插入数组

c++ - 切换到数组上的 vector 时位图加载中断

javascript - 在 iOS 上同时播放 Html5 视频和音频?

javascript - 理解此语法 if(obj[x])

javascript - 如何重命名原型(prototype)?

javascript - 如何在 Protractor 测试中访问 $localStorage 给出 WebDriverError

javascript - Angularjs 在不同页面上的服务之间传递数据

php - 转储到 JavaScript 数组的 HTML 文本破坏了我的代码