首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过滤在排除DOM内容时不起作用

过滤在排除DOM内容时不起作用
EN

Stack Overflow用户
提问于 2017-10-27 09:10:56
回答 1查看 56关注 0票数 2

我正在构建两个AngularJS (版本1.6.5)组件,当我使用transclusion时,我无法实现过滤。

第一个组件是一个简单的容器,它使用transclusion填充<div>内容:

代码语言:javascript
复制
app.component('panelWithTitle', {
  template: '<div><h1>{{$ctrl.title}}</h1><div class="content" ng-transclude></div></div>',
  bindings: {
    title: '@'
  },
  require: 'title',
  transclude: true
});

第二个组件使用容器(<panel-with-title>),并向它提供一个简单的筛选(来自输入字段)列表:

代码语言:javascript
复制
app.component('mainComponent', {
  controller: function($scope) {
    $scope.allItems = ["10", "100", "200", "42", "1337"];
    // Simple filter function (may be more complicated)
    $scope.filterFunc = function (item) {
      if (!$scope.filterValue) {
        // No value
        return true;
      }
      return item.indexOf($scope.filterValue) !== -1;
    };
  },
  template: '<panel-with-title title="MyTitle">'            // Replace by <div>
      + '<input type="text" ng-model="filterValue">'
      + '<ul><li ng-repeat="item in allItems | filter:filterFunc">{{item}}</li></ul>'
      + '</panel-with-title>'                               // Replace by </div>
});

在这种状态下,由于$scope.filterValue未定义,过滤无法工作。这是一个演示Plunkr。我注意到:

  • 如果我不使用transclusion (例如:如果我用简单的<panel-with-title>标记替换<div>标记),则筛选是有效的。
  • 无论如何,$scope.allItems是正确定义的。

我做错什么让它不起作用了?为什么$scope.filterValue是未定义的,而$scope.allItems是定义的?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-10-27 11:18:11

如果您不想编写更多的代码(filterFunc函数),那么

您应该通过模型filterValue将此代码连接到直接过滤器的模型(ng- model =“”)。请找到我下面的柱塞链接-

http://plnkr.co/edit/sp9XFUMXLdmSwESujMQD?p=preview

代码语言:javascript
复制
app.component('mainComponent', {
  controller: function($scope) {
    $scope.allItems = ["10", "100", "200", "42", "1337"];
  },
  template: '<panel-with-title title="MyTitle">'            // Replace by <div>
      + '<input type="text" ng-model="filterValue">'
      + '<ul><li ng-repeat="item in allItems | filter:filterValue">{{item}}</li></ul>'
      + '</panel-with-title>'                               // Replace by </div>
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46971450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档