javascript AngularJS ng-repeat 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17955244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS ng-repeat not working?
提问by accraze
I'm new to AngularJs and still trying to figure out how the basics work... I'm using the Soundcloud API to pull a list of followers for a given user. So far in my $scope.init
function I am able to connect to Soundcloud, authenticate a user and return a json list of the user's followers. I then push each follower into an array called $scope.results
and verify that the array is full by outputting it in the console. However when I try to output each follower as a list item in the array using ng-repeat
in my main.html
view and I get nothing..... here's my code:
我是 AngularJs 的新手,仍然试图弄清楚基本原理是如何工作的……我正在使用 Soundcloud API 来为给定用户拉取关注者列表。到目前为止,在我的$scope.init
函数中,我能够连接到 Soundcloud,对用户进行身份验证并返回用户关注者的 json 列表。然后我将每个关注者推送到一个名为$scope.results
的数组中,并通过在控制台中输出它来验证该数组是否已满。但是,当我尝试将每个关注者作为数组中的列表项输出ng-repeat
到我的main.html
视图中时,我什么也没得到......这是我的代码:
main.js
主文件
.controller('MainCtrl', function ($scope, $http) {
$scope.apiKey = "##########################";
$scope.results = [];
$scope.init = function(){
SC.initialize({
client_id: $scope.apiKey,
redirect_uri: "http://localhost:9000/callback.html"
});
// initiate auth popup
SC.connect(function() {
SC.get('/me', function(me) {
alert('Hello, ' + me.username);
});
SC.get('/me/followers', function(followers) {
//console.log(followers);
//angular.forEach(followers, function(value, index){
angular.forEach(followers, function(follower, index){
$scope.results.push(follower);
});
console.log($scope.results);
});
});
main.html// which is a view
main.html// 这是一个视图
`<div ng-init="init()">`
`<li ng-repeat="follower in results">`
`<div class="row-fluid">`
`<div class="span3">`
`<div class="span6">`
<h3>{{follower.username}}</h3>
<p>{{follower.description}}</p>
</div>
</div>
</div>
</li>
</div>
index.html
索引.html
<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<!-- build:css styles/main.css -->
<link rel="stylesheet" href="styles/bootstrap.css">
<link rel="stylesheet" href="styles/main.css">
<!-- endbuild -->
</head>
<body ng-app="soundSelectApp" ng-controller="MainCtrl">
<!--[if lt IE 7]>
<p class="chromeframe">You are using an outdated browser. <a href="http://browsehappy.com/">Upgrade your browser today</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to better experience this site.</p>
<![endif]-->
<!--[if lt IE 9]>
<script src="components/es5-shim/es5-shim.js"></script>
<script src="components/json3/lib/json3.min.js"></script>
<![endif]-->
<!-- Add your site or application content here -->
<div class="container" ng-view></div>
<script src="components/angular/angular.js"></script>
<script src="components/angular-resource/angular-resource.js"></script>
<script src="components/angular-cookies/angular-cookies.js"></script>
<script src="components/angular-sanitize/angular-sanitize.js"></script>
<!-- build:js scripts/scripts.js -->
<script src="scripts/app.js"></script>
<script src="scripts/controllers/main.js"></script>
<script src="http://connect.soundcloud.com/sdk.js"></script>
<!-- endbuild -->
<!-- Google Analytics: change UA-XXXXX-X to be your site's ID. -->
<script>
var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
s.parentNode.insertBefore(g,s)}(document,'script'));
</script>
回答by Ajay Beniwal
You are missing a call to $apply() because you are doing work outside of the angular world so try to add one line below push command and it should work
您错过了对 $apply() 的调用,因为您正在角度世界之外进行工作,因此请尝试在 push 命令下方添加一行,它应该可以工作
angular.forEach(followers, function(follower, index){
$scope.results.push(follower);
});
$scope.$apply()