gpt4 book ai didi

javascript - Angular ng-repeat 标签不起作用

转载 作者:行者123 更新时间:2023-11-29 21:50:07 25 4
gpt4 key购买 nike

我正在玩一个简单的 Angular 示例,但我还没有弄清楚为什么我的 ng-repeat 指令不起作用。下面的代码片段只是一个连接到 github 以拉回用户和 repo 数据的 Controller 。

我可以确认我正在取回用户和 repo 数据,它的形状符合我的预期,并且 repo 集合在 onRepos() 返回时存在于 $scope 中。我似乎无法使用 ng-repeat 获取要呈现的 repo 名称。我在控制台中没有看到任何错误,它只是不想工作。

在过去的 30 分钟里,我一直盯着这个看,但无法弄明白。我错过了什么?

Angular 1.3.15

应用程序.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 中。

关于javascript - Angular ng-repeat 标签不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29714383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com