【问题标题】:Filter between two components AngularJS过滤两个组件AngularJS
【发布时间】:2018-11-09 07:18:29
【问题描述】:

我知道我可以这样过滤:

<input type="search" ng-model="searchTable">
<table>
  <thead>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="row in $ctrl.rows | filter: searchTable">
      <td>{{ row.firstName }}</td>
      <td>{{ row.lastName }}</td>
    </tr>
  </tbody>
</table>

如果&lt;input type="search" ng-model="searchTable"&gt;&lt;tr ng-repeat="row in $ctrl.rows | filter: searchTable"&gt; 位于不同的组件中,我该如何过滤?

这是一个简单的fiddle

据我了解,我需要将$onChanges() 和表达式绑定bindings: { onChange: '&amp;' } 添加到searchComponent,但不完全了解如何实现它。

谢谢

【问题讨论】:

    标签: javascript angularjs binding components angularjs-filter


    【解决方案1】:

    您可以在组件中尝试 emit - on 进行通信,如下面的代码所示。也请检查您更新的jsfiddle 与您给定场景的工作示例。

    搜索组件控制器:

    $scope.$watch('searchTable', function() {
      $rootScope.$emit('onEmitSearchData', $scope.searchTable);
    });
    

    表格组件控制器:

    $rootScope.$on('onEmitSearchData', function(event, data) {
      $scope.searchTable = data;
    });
    

    【讨论】:

      【解决方案2】:

      Here 是工作小提琴。您将搜索词存储在父组件中并将其传递给两个子组件。

      var app = angular.module('app', []);
      
      app.controller('appController', function() {
      	this.search = "";
      });
      
      app.component('searchComponent', {
      	template: `
        	<h4>{{ $ctrl.title }}</h4>
          <label>Search table</label>
          <input type="search" ng-model="$ctrl.search">
        `,
        controller: function() {
        	this.title = 'Search Component';
        },
        bindings: {
        	search: "="
        }
      });
      
      app.component('tableComponent', {
      	template: `
        	<h4>{{ $ctrl.title }}</h4>
          <!-- <p>This works</p> 
          <label>Search table</label>
          <input type="search" ng-model="searchTable"> -->
          <table>
          	<thead>
            	<tr>
              	<th>First Name</th>
                <th>Last Name</th>
              </tr>
            </thead>
          	<tbody>
            	<tr ng-repeat="row in $ctrl.rows | filter: $ctrl.search">
              	<td>{{ row.firstName }}</td>
                <td>{{ row.lastName }}</td>
              </tr>
            </tbody>
          </table>
        `,
        controller: function() {
       		this.title = 'Table Component';
          
          this.rows = [
          	{firstName: 'Zulu', lastName: 'Apple'},
            {firstName: 'Alice', lastName: 'xRay'},
            {firstName: 'Fred', lastName: 'Rogers'}
          ];
        },
        bindings: {
        	search: "<"
        }
      });
      body {
        font-family: 'Arial', sans-serif;
      }
      table {
        border-collapse: collapse;
      }
      td, th {
        border: 1px solid grey;
        padding: 5px;
      }
      label {
        display: block;
      }
      input {
        margin-bottom: 10px;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.0/angular.min.js"></script>
      <div ng-app="app">
        <div ng-controller="appController">
          <h3>Filter Between Components</h3>
          <search-component search="search"></search-component>
          <table-component search="search"></table-component>
        </div>
      </div>

      【讨论】:

        猜你喜欢
        • 2015-09-28
        • 1970-01-01
        • 1970-01-01
        • 2017-06-08
        • 2020-03-21
        • 1970-01-01
        • 2014-08-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多