2015-04-18 59 views
2

我玩弄一个简单的角度的例子,我还没有弄清楚为什么我的ng重复指令不工作。下面的代码片段仅仅是一个控制器,它向github提供了将用户和回购数据返回的信息。角ng重复标记不工作

我可以确认我收到了用户和回购数据,它的形状和我期望的一样,并且当onRepos()返回时,回收集合存在于$ scope中。我似乎无法得到使用ng-repeat渲染的repo名称。我在控制台中看不到任何错误,它只是不想工作。

我已经花了最后的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> 

回答

2

看来的MainController范围之外的table标签。将其移动到包含ng-controller属性的div中。

+0

它就在那里。我不知道为什么没有发生在我身上。 – nerraga