2016-04-11 44 views
0

我正在嘗試檢索遠程服務器上的文件列表,並將它們顯示在網頁上。 作用域變量fileListOnCTP包含從網頁訪問的文件列表。但是網頁不顯示文件列表。但是ng-inspector顯示了保存正確值的變量。有人能告訴我我哪裏可能會出問題嗎?無法從HTML中訪問範圍變量

$scope.listOfFilesOnCTP = ""; 
 
\t $scope.fileListOnCTP = ""; 
 
\t $scope.GetListOfFilesonCTP = function(path){ 
 
\t $scope.listOfFilesOnCTP = RdaService.getListOfFilesonCTP(encodeURIComponent(path)).then(function(response){ 
 
\t \t \t var newData = response.data; 
 
\t \t \t console.log(newData); 
 
\t \t \t return newData; 
 
\t \t }); 
 
\t console.log($scope.listOfFilesOnCTP); // --> Displaying the file list correctly 
 
\t \t //path = path.replace(/\//g, '_'); 
 
\t $scope.fileListOnCTP = $scope.listOfFilesOnCTP; 
 
\t };
\t <div class="col-md-3" id="CTP Jobs"> 
 
\t \t <h3>JOBS</h3> 
 
\t \t <table class="table table-striped"> 
 
\t \t \t <div ng-model="fileListOnCTP" ng-init="GetListOfFilesonCTP('/home/topas/rda_app/JOBS')"> 
 
\t \t \t \t <span><tr>{{fileListOnCTP}} <!-- Variable not getting resolved although it shows correct value in controller --> 
 
\t \t \t \t \t </tr></span> 
 
\t \t \t </div> 
 
\t \t </table> 
 
\t </div>

+1

,你不能只是簡單的做一個'ngModel'上一個DIV,只有輸入/ textarea的(除非您使用需要'ngModel'一個指令) –

+0

如果返回值是一個數組,你可能想要一個ng-repeat。什麼是fileListOnCTP的類型? – BobS

+0

感謝您的反饋..但我沒有運氣,沒有使用ng-model。 –

回答

2

由於您RdaService.getListOfFilesonCTP回報的承諾,你需要更新then內的$scope變量,因爲這是在那裏你可以得到承諾的解析值。如果您嘗試在其他地方設置$scope變量,則不能保證該服務會返回一個值,這就是您以前的代碼無法工作的原因。

參考:https://docs.angularjs.org/api/ng/service/$q#the-promise-api