【问题标题】:HTML Table data not being displayed when used ng-repeat使用 ng-repeat 时不显示 HTML 表格数据
【发布时间】:2017-06-29 06:17:56
【问题描述】:

我正在编写一个简单的 Flask 网络应用程序,我正在尝试使用 Angular js 在我的 html 页面上显示一个简单的表格数据,我正在使用 ng-repeat 循环数据,但由于未知原因它无法正常工作,当我在控制台上检查时,它没有显示任何错误。 请帮忙!

这是我的 HTML 代码

<tbody>     
  <!-- {% for user in User %}-->
  <tr ng-repeat="user in User">
    <td> // user.uid // </td>
    <td >// user.taskname // 
      <input type="image" class="deleteImg" src="static/img/trash_can.png" height="15px" width="18px" name="removeId" value="{{user.uid}}" > 
      <input type="image" value="{{user.uid}}" src="static/img/editbtn.svg" height="15px" width="18px" name="editId">   
    </td>
  </tr>
  <!-- {% endfor %} -->
</tbody>

这是我的角码

<script>   
  var app = angular.module("app", []);
  app.controller('myctrl', function($scope) {
    $scope.myname="Howdy"; 
    $scope.transfer= function(user) {
      $scope.maintxt="kkk";
    };   
  });
  app.config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('//').endSymbol('//');
  });
</script>  

【问题讨论】:

  • User 里面有什么?它似乎不在您的控制器中。
  • @Koralarts 是用户模型类。我正在使用 Flask Python 框架。
  • 这就是 ng-repeat 不起作用的原因,它试图循环一个在 Angular 范围内不存在的变量。您可以通过 Javascript 手动(不推荐)将 User 传递给 Angular,或者调用返回您的用户列表的 API。
  • @Koralarts 好的,我该怎么做?你能给我一个想法或快速教程的链接吗?

标签: html angularjs angularjs-ng-repeat


【解决方案1】:

Flask Restful API:https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask

AngularJS 使用 Restful API:https://spring.io/guides/gs/consuming-rest-angularjs/

烧瓶:

@app.route(path, methods=['GET'])
def get_User():
  get list of user
  if list of user is empty 
    return 404
  return list of user as JSON

AngularJS:

controller $scope, $http
  $scope.User = []
  $http.get(APIEndPoint)
    .then((response) => {
      $scope.User = response.User
    })

HTML:

<tr ng-repeat="user in User">
  process user here
</tr>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多