2015-11-05 48 views
1

我包括用在我的主頁使用

<div ng-controller="detailViewCtrl" ng-include="'partials/template.html'"></div> 

這個劇本里面有表現出一些文件列表的HTML文件。使用一個按鈕,是另一個模板內,包括在幾乎相同的方式

<li ng-repeat="document in relatedDocuments" style="cursor: pointer;" class="box" ng-click="gotoDocument(document)"> 
    <div><i class="fa fa-file-pdf-o"></i> <span>{{document.name | limitTo: 20 }}{{document.name.length > 20 ? '...' : ''}}</span></div> 
    <md-tooltip md-direction="top"> 
    {{document.name}} 
    </md-tooltip> 
    <thumbnail file-type="{{document.filetype}}" source="document.fullpath" max-height="150" max-width="210"></thumbnail> 
</li> 

在某一點上,我可能上傳新的PDF文件。該文件上傳到我的服務器,並進行查詢請求文檔列表(現在預計包含這個新文檔)。該查詢與最初調用填充relatedDocuments的功能相同。這次雖然列表沒有更新,但無論我上傳了多少文檔。只有當我刷新時,我纔會看到新上傳的文件。

試圖強制$digest我自己不會產生任何效果,即使在解決承諾後我可以在控制檯中看到打印列表,我的視圖仍然保持不變。

控制器看起來是這樣的:

.controller('detailViewCtrl', function($scope, $i18next, $routeParams,...  

$scope.relatedDocuments = []; 

$scope.getRelatedDocuments = function(document) { 
    myAPI.getRelatedDocuments($scope.id).then(function(response) { 
    $scope.relatedDocuments = response.data.result;  
    console.log("getRelatedDocuments", $scope.relatedDocuments); 
    }); 
}; 

$scope.getRelatedDocuments(); 

而且這個模塊內部有一個uploadDocument()功能,它上傳文檔後,它調用getRelatedDocuments()。該功能使用ng-file-upload來處理到服務器的實際數據傳輸。

$scope.uploadFile = function(file) { 
    file.upload = Upload.upload({ 
     url: tempPath, 
     method: 'POST', 
     file: file, 
     sendFieldsAs: 'form', 
     fields: { 
     docid: $scope.id 
     } 
    }); 

    file.upload.then(function(response) { 
     $timeout(function() { 
     file.result = response.data; 
     }); 
    }, function(response) { 
     $scope.getRelatedDocuments(); // Here I try to update the list 
     if (response.status > 0) 
     $scope.errorMsg = response.status + ': ' + response.data; 
    }, function(evt) { 
     file.progress = Math.min(100, parseInt(100.0 * evt.loaded/evt.total)); 
    }); 
    } 

這個上傳功能是在主頁面某處按下相應的按鈕後調用的。由於我可以看到日誌消息,並且我認爲所有事情都應該如此。

包含代碼相關部分的plunk可以找到here。它不需要詛咒,因爲它需要後端來保存並獲取文檔列表,但它仍然是一個很好的參考。

有誰知道爲什麼會發生這種情況?

+0

你能發表一些'detailViewCtrl'的代碼嗎?另外,也許你試着用'$ watch'函數來檢測這個變化,然後使用'$ apply()'函數。 –

+0

你能提供樣品plunkr嗎? – Grundy

+0

我知道它在這類問題中確實非常有用,可以提供一個示例,但在這種情況下,我不確定我可以做什麼,因爲我已經添加了很多東西,並且存在類似於調用我的服務器的東西,我無法複製在一個簡單的普拉克。我希望這是我用包含模板或異步調用的方式犯的一些錯誤,因爲在過去,我從來沒有在模型更改上更新過頁面的問題。所有與該問題相關的代碼都包含在內。 – dearn44

回答

3

看起來我不應該使用ng-controller指令來包含這兩個模板,因爲它們是從也使用相同控制器的頁面調用的。如果你有相同的控制器嵌套到自己,那麼Angular可能會以意想不到的方式行事。