简体   繁体   English

即使列表是ngRepeat也不会更新

[英]ngRepeat not updating even though list is

I'm having a problem where despite a list being updated, ngRepeat does not display the information. 我遇到一个问题,尽管列表已更新,但ngRepeat却不显示信息。

In the code below, the $scope.selectedImages array is being added to when images are selected using the file input. 在下面的代码中,使用文件输入选择图像时,将添加$scope.selectedImages数组。 It is set up so that only unique files will be added to the list of selectedImages (no duplicates). 设置它的目的是仅将唯一的文件添加到selectedImages列表中(不重复)。

At the end of the function, scope.selectedImages prints the array with the new values, but in the view, ngRepeat does not add the new list items with the image names. 在函数末尾, scope.selectedImages使用新值打印数组,但是在视图中,ngRepeat不会添加带有图像名称的新列表项。

Other questions regarding this issue said that $scope.$apply should fix the problem, but for me it is not. 关于这个问题的其他问题说$scope.$apply应该可以解决问题,但对我来说不是。 Am I using it wrong or is there something else that I am misunderstanding. 我使用的是错误的还是其他误解的信息?

EDIT: View 1 and View 2 are in separate HTML pages that both rely on the same controller. 编辑: 视图1视图2在单独的HTML页面中,它们都依赖于同一控制器。

View 1: 查看1:

<input type="file" multiple accept="image/*" class="file-input" id="img-upload-btn" onchange="angular.element(this).scope().select(this)">
<md-button class="md-primary md-raised sidenav-btn" ng-click="proxy('img-upload-btn')">
  Select <strong>Images</strong>
</md-button>

The proxy function allows the md-button to click the input. 代理功能允许md-button单击输入。

View 2: 查看2:

<div layout-padding flex ng-controller="upload-controller">
  <ul>
    <li ng-repeat="im in selectedImages">{{im.name}}</li>
  </ul>
</div>

Controller: 控制器:

$scope.selectedImages = [];
$scope.select = function(element) {
  $scope.$apply(function(scope) {
    var justFiles = $.map(element.files, function(val, key) {
      return val;
    }, true);

    var fileEquality = function(f1, f2) {
      if (f1.name != f2.name) return false;
      if (f1.size != f2.size) return false;
      if (f1.type != f2.type) return false;
      if (f1.lastModified != f2.lastModified) return false;
      return true;
    }

    // inefficient, find better way later
    for (i in justFiles) {
      var contains = false;
      var file = justFiles[i];
      for (i in scope.selectedImages) {
        if (fileEquality(file, scope.selectedImages[i])) {
          contains = true;
          break;
        }
      }
      if (!contains) scope.selectedImages.push(file);
    }
    console.log(scope.selectedImages);
  });
};

Your input field template and controller template have to different scopes. 您的输入字段模板和控制器模板具有不同的范围。 Use $rootScope.selectedImages in your case. 在您的情况下使用$rootScope.selectedImages

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM