簡體   English   中英

AngularJS ng-repeat不起作用?

[英]AngularJS ng-repeat not working?

我是AngularJs的新手,並且仍在試圖弄清楚基礎知識是如何工作的...我正在使用Soundcloud API為給定用戶提取關注者列表。 到目前為止,在我的$scope.init函數中,我能夠連接到Soundcloud,驗證用戶並返回用戶關注者的json列表。 然后我將每個關注者推送到一個名為$scope.results的數組中,並通過在控制台中輸出它來驗證數組是否已滿。 但是,當我嘗試在我的main.html視圖中使用ng-repeat輸出每個關注者作為數組中的列表項時,我什么都沒得到......這是我的代碼:

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 //這是一個視圖

`<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

<!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>

你錯過了對$ apply()的調用,因為你在角度世界之外的工作,所以嘗試在push命令下添加一行,它應該工作

 angular.forEach(followers, function(follower, index){
                $scope.results.push(follower);
            });

$scope.$apply()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM