1

我在我的主页中使用了一个 html 文件

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

在这个脚本中有一个显示一些文档的列表。

<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 文件。该文件被上传到我的服务器并进行查询以请求文档列表(现在预计将包含此新文档)。此查询与最初调用 fill 的函数相同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 。curse 不起作用,因为它需要后端保存和获取文档列表,但它仍然是一个很好的参考。

有谁知道为什么会这样?

4

1 回答 1

3

似乎我不应该在包含两个模板时使用 ng-controller 指令,因为它们是从也使用相同控制器的页面调用的。如果您将相同的控制器嵌套在自身上,那么 Angular 可能会以意想不到的方式运行。

于 2015-11-06T16:40:26.607 回答