2014-02-10 118 views
1

我有對象的數組稱爲presentations角選擇陣列

我想在select元素使用它們,但它不工作。選擇框只是空白而沒有任何內容。

我想這可能是因爲它想要一個數組或對象,而不是一個對象數組。

我做:

<select ng-model="secondPres" ng-options="p.name for p in presentations" ></select> 

我可以CONSOLE.LOG我presentations陣列和得到這個:

0: Object 
name: "club" 
origName: "club" 
slide: "1" 
tenant: "grant" 

1: Object 
name: "2slide" 
origName: "club" 
slide: "1" 
tenant: "grant" 

2: Object 
name: "CT_Stress_Test" 
origName: "club" 
slide: "1" 
tenant: "grant" 

那麼這是怎麼回事?

這是我的控制器代碼。產生的console.log顯示我上面的內容是在底部。擴大()調用data-ng-init

var socket = io.connect('https://xxxxxxx.xxxxxxxxxxxxx.net:3000'); 

$scope.versions =[]; 
$scope.presentations=[]; 
$scope.secondPres; 
$scope.expand = function(){ 
    console.log("expand hit " + presentation1); 
    var urlToSign = presentation1; 
    var urlToSign = urlToSign.replace(/^(.*)\/Slide\d_v\d.PNG(.*)/,"$1"+""); 
    console.log("url to sign = " + urlToSign); 
    socket.emit('getSignedSlidesFromUrl',urlToSign); 
    socket.on('signedUrls',function(data){ 
     console.log('got back signedUrls' + data); 
     $scope.slides=data; 
     $scope.$apply(); 

     var re = /^.*\/\/.*\/.*\/(.*)/; 
     var fileName = re.exec(urlToSign)[1]; 

     for(i=0;i<presentationList.length;i++){ 
      var url = presentationList[i]; 
      console.log(url); 
      var re = /^.*\/\/.*\/(.*)\/.*$/; 
      var re2 = /^.*\/\/.*\/(.*)\/.*\//; 
      var re3 =/^.*\/\/.*\/.*\/Slide(\d*)/; 
      var fileName2 = re.exec(url)[1]; 
      var customerName = re2.exec(url)[1]; 
      var slideNum = re3.exec(url)[1]; 

      $scope.presentations.push({name:fileName2, origName:fileName, tenant:customerName, slide:slideNum}); 
     } 
     console.log($scope.presentations); 
    }); 
}; 
+0

你能發佈你的控制器代碼嗎? –

+0

這個數組是否來自外部服務而沒有範圍消化? – lib3d

+0

@MikeRobinson完成。 – Houseman

回答

1

你只是缺少一個$scope.$apply()後您for循環,使角度看到你的新的數組項。或者將事件處理程序的全部內容封裝到傳遞到$scope.$apply()的函數中,類似於此處的snippit。

socket.on('signedUrls',function(data){ 
    $scope.$apply(function() { 
     console.log('got back signedUrls' + data); 
     $scope.slides=data; 

     var re = /^.*\/\/.*\/.*\/(.*)/; 
     var fileName = re.exec(urlToSign)[1]; 

     for(i=0;i<presentationList.length;i++){ 
      var url = presentationList[i]; 
      console.log(url); 
      var re = /^.*\/\/.*\/(.*)\/.*$/; 
      var re2 = /^.*\/\/.*\/(.*)\/.*\//; 
      var re3 =/^.*\/\/.*\/.*\/Slide(\d*)/; 
      var fileName2 = re.exec(url)[1]; 
      var customerName = re2.exec(url)[1]; 
      var slideNum = re3.exec(url)[1]; 

      $scope.presentations.push({name:fileName2, origName:fileName, tenant:customerName, slide:slideNum}); 
     } 
     console.log($scope.presentations); 
    }); 
    }); 
+1

我剛剛在我的方法的末尾添加了'$ scope。$ apply()',因爲我只打算在這個套接字上打一次。 – Houseman