javascript - 关于过滤器输入和重置索引的 ng-change 的 Material 虚拟重复更新列表?

标签 javascript angularjs object angular-material

我已在此 plnkr 中复制了该场景:

<input type="text" ng-model="search.id" ng-change="ctrl.infiniteItems.fetchMoreItems_()">

var vm = this;
  vm.infiniteItems = {
    numLoaded_: 0,
    toLoad_: 0,
    items: [],

    // Required.
    getItemAtIndex: function(index) {
      if (index > this.numLoaded_) {
        this.fetchMoreItems_(index);
        return null;
      }

      return this.items[index];
    },

    // Required.
    getLength: function() {
      return this.numLoaded_ + 3;
    },

    fetchMoreItems_: function(index) {
      if (this.toLoad_ < index) {
        this.toLoad_ += 10;
        var filtered = [];
        fetcher.fetch($scope.search.id).then(angular.bind(this, function(obj) {
          this.items = obj;
          this.numLoaded_ = this.toLoad_;
          if (obj.length <= this.toLoad_) {
            this.items = this.items.concat(obj);
            this.numLoaded_ = this.toLoad_;
          }
        }));
      }
    }
  }

https://plnkr.co/edit/vnrEOmQJ96ZETQpzlaMj

我想在输入值时更新 View ,现在我可以过滤,但只有当我滚动时才得到结果,它从 Controller 调用方法并在顶部显示结果,并且永远不会停止滚动,返回空行一旦结果结束。

我希望它在发生这种情况时停止,就像它在没有过滤器的情况下一样。

最佳答案

这是你想要的吗? -Plunker

标记

<input type="text" ng-model="search.id" ng-change="inputChange()">

JS

vm.infiniteItems = {
    numLoaded_: 0,
    toLoad_: 0,
    items: [],

    // Required.
    getItemAtIndex: function(index) {
      if (index > this.numLoaded_) {
        this.fetchMoreItems_(index);
        return null;
      }

      return this.items[index];
    },

    // Required.
    getLength: function() {
      return this.numLoaded_ + 3;
    },

    fetchMoreItems_: function(index) {
      if (this.toLoad_ < index) {
        this.toLoad_ += 10;
      }
    }
};

$scope.inputChange = function () {
    console.log($scope.search.id);
     fetcher.fetch($scope.search.id).then(angular.bind(this, function(obj) {
         console.log(obj);
         vm.infiniteItems = obj;
    }));
}

关于javascript - 关于过滤器输入和重置索引的 ng-change 的 Material 虚拟重复更新列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39290748/

相关文章:

javascript - ASP.NET MVC 3 中有类似 .js.erb 模板的东西吗?

javascript - Yammer 无法使用 js 工作

angularjs + bootstrap ui + $modal -> 只在第一次显示

javascript - 为所有被拒绝的 promise 调用相同的函数

Javascript将多个对象插入一个空对象

JavaScript 加载顺序

Javascript反向匹配过程

javascript - 需要帮助分离我的项目。 Angularjs+PHP+MySQL

javascript - Object.freeze 不会阻止对象被重新初始化?

java - 了解对象初始化中的泛型