角度ng-repeat标签不起作用

时间:2015-04-18 07:33:30

标签: javascript angularjs angularjs-ng-repeat ng-repeat

我用一个简单的角度例子来玩弄我并且我还没弄清楚为什么我的ng-repeat指令不起作用。下面的代码片段只是一个控制器,可以通过github将用户和回购数据拉回来。

我可以确认我同时获得了用户和回购数据,它形成了我期望的方式,并且当onRepos()返回时,repos集合存在于$ scope中。我似乎无法使用ng-repeat来获取回购名称。我没有在控制台中看到任何错误,它只是不想工作。

我花了最后30分钟盯着这个并且无法弄明白。我错过了什么?

角度1.3.15

app.js

(function(){
  var app = angular.module('githubviewer',[]);

  var MainController = function($scope, $http) {

    var onUserComplete = function(response) {
      $scope.user = response.data;
      $http.get($scope.user.repos_url)
        .then(onRepos, onError);
    }

    var onRepos = function(response) {
      $scope.repos = response.data;
      console.log($scope.repos[0].name);
    }

    var onError = function(response) {
      $scope.error = response.data;
    }

    $scope.search = function(username) {
      var url = "https://api.github.com/users/" + username;
      $http.get(url).then(onUserComplete, onError);
    }

    $scope.message = "github viewer";
    $scope.usename = "angular";
  }

  app.controller("MainController", ["$scope", "$http", MainController ]);
}());

的index.html

<!DOCTYPE HTML>
<html ng-app="githubviewer">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>Angular JS - Getting Started</title>
  <script src="angular.js" type="text/javascript" charset="utf-8"></script>
  <script src="app.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>

  <div ng-controller="MainController">
    <div> {{message}} </div>
    <div> {{error}} </div>
     <form name="searchUser" ng-submit="search(username)">
      <input type="search" required placeholder="Username to find" ng-model="username">
      <input type="submit" value="search">
    </form>  
    <div>
      <h2> Name : {{user.name}} </h2>
      <img ng-src="{{user.avatar_url}}" title="{{user.name}}">
    </div>
  </div>

  Repos: {{repos.length}} 
  <table>
    <thead>
      <tr>
        <th>Name</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="repo in repos">
        <td>{{ repo.name }}</td>
      </tr>
    </tbody>
  </table>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

似乎table范围之外的MainController标记。将其移动到包含ng-controller属性的div中。