Angular ng-repeat 标签不工作
Angular ng-repeat tag not working
我正在尝试一个简单的 angular 示例,但我还没有弄清楚为什么我的 ng-repeat 指令不起作用。下面的代码片段只是一个连接到 github 以拉回用户和回购数据的控制器。
我可以确认我正在取回用户和回购数据,它的形状符合我的预期,并且回购集合在 onRepos() returns 时存在于 $scope 中。我似乎无法使用 ng-repeat 获取要呈现的回购名称。我在控制台中没有看到任何错误,它只是不想工作。
我花了最后 30 分钟盯着这个,但无法弄清楚。我错过了什么?
angular 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>
您的 table
标签似乎超出了 MainController
范围。将其移动到包含 ng-controller
属性的 div 内。
我正在尝试一个简单的 angular 示例,但我还没有弄清楚为什么我的 ng-repeat 指令不起作用。下面的代码片段只是一个连接到 github 以拉回用户和回购数据的控制器。
我可以确认我正在取回用户和回购数据,它的形状符合我的预期,并且回购集合在 onRepos() returns 时存在于 $scope 中。我似乎无法使用 ng-repeat 获取要呈现的回购名称。我在控制台中没有看到任何错误,它只是不想工作。
我花了最后 30 分钟盯着这个,但无法弄清楚。我错过了什么?
angular 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>
您的 table
标签似乎超出了 MainController
范围。将其移动到包含 ng-controller
属性的 div 内。