2015-10-26 100 views
-1

當WebSocket的消息收到以下代碼運行:AngularJS陣列NG-重複

connection.onmessage = function (eventInfo) { 
var onConnectionMessage = JSON.parse(eventInfo.data); 

if (onConnectionMessage.messageType === "newRequest") { 
    getQuizRequests(); 
} 
} 

getQuizRequests()是下面的函數:

function getQuizRequests() { 
var URL = '/acceptOrReject/' + lookUpCode(); 

$http.get(URL) 
    .success(function (data) { 
     for (var i = 0; i < data.teamArray.length; i++) { 
      teamArray[0] = data.teamArray[i]; 
     } 
    }) 
    .error(function (data, status) { 
     alert("ERROR data cant be loaded"); 
    }); 
} 

我想調用此teamArray在NG -重複。如何將這個數組填充到我使用ng-repeat的代碼中?

+1

需求是查看呈現控制器範圍的一部分。 'teamArray'定義在哪裏?你爲什麼每次都寫'teamArray [0]'。請提供所有相關的代碼上下文 – charlietfl

+0

此代碼是否在服務中?控制器? – Starscream1984

+0

'teamArray [0] = data.teamArray [i];'沒有意義 –

回答

0

假設這一切都是在同一個控制器

$scope.teamArray = []; 

function getQuizRequests() { 
    var URL = '/acceptOrReject/' + lookUpCode(); 

    $http.get(URL) 
    .success(function (data) { 
     for (var i = 0; i < data.teamArray.length; i++) { 
      $scope.teamArray.push(data.teamArray[i]); 
     } 
    }) 
    .error(function (data, status) { 
      alert("ERROR data cant be loaded"); 
    }); 
} 

然後在你的HTML

<ul ng-show="teamArray.length > 0" ng-repeat="member in teamArray"> 
    <li>{{member}}</li> 
</ul>