简体   繁体   English

如何从Angular 6中的另一个组件获取[(ngmodel)]

[英]how to get [(ngmodel)] from another component in Angular 6

I'm searching for the products with the pipe. 我在寻找带有管子的产品。 The pipe is working when [(ngModel)] in product.component.html but is not working when [(ngModel)] in app.component.html. 该管道在product.component.html中的[[ngModel)]时起作用,但在app.component.html中的[(ngModel)]时则不起作用。

product-find.pipe.ts: product-find.pipe.ts:

import { Pipe, PipeTransform} from '@angular/core';
import { Product } from './product/product';

@Pipe({
  name: 'productFind'
})

export class ProductFindPipe implements PipeTransform {

  transform(value: Product[], filterText?: string): Product[] {
    filterText = filterText ? filterText.toLocaleLowerCase() : null;
    console.log(filterText);
    return filterText ? value.filter((x: Product) => x.productName.toLocaleLowerCase().indexOf(filterText) >= 0) : value;
  }

}

app.component.html: app.component.html:

...

<input [(ngModel)]="filterText" name="filterText" class="form-control" type="search" placeholder="Search" aria-label="Search">

...

product.component.html: product.component.html:

<div>
  <input class="form-control" type="search" placeholder="Search" aria-label="Search">
</div>
<div>
  <ul class="list-group">
    <li *ngFor="let product of products |productFind:filterText" class="list-group-item">
      ...
    </li>
  </ul>
</div>

How to fix this? 如何解决这个问题?

Thanks. 谢谢。

You need declare @Input decorator inside product.component.ts file. 您需要在product.component.ts文件中声明@Input装饰器。

In product.component.ts: 在product.component.ts中:

import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-product',
  templateUrl: 'your-template-url-here
})
export class ProductComponent {

    @Input() filterText: any; 

        //rest of your code here
}

In product.component.html 在product.component.html中

<div>
    <ul class="list-group">
       <li *ngFor="let product of products |productFind:filterText" class="list-group-item">
                        ...
        </li>
    </ul>
</div>

Now in you app.component.html like: 现在在您的app.component.html中,如下所示:

<input [(ngModel)]="filterText" name="filterText" class="form-control" type="search" placeholder="Search" aria-label="Search">
<app-product [filterText]="filterText"><app-product>

Hope this will work for you!!!! 希望这对您有用!!!

Two possible ways to your problem: 解决问题的两种可能方法:

  1. Define filterText in product.component.ts, and use [(ngModel)]="filterText" in product.component.html. 在product.component.ts中定义filterText,并在product.component.html中使用[(ngModel)] =“ filterText”。 Remove input box from app.component. 从app.component中删除输入框。

  2. Pass the filterText variable of parent component ie app.component, to child product.component using @Input 使用@Input将父组件(即app.component)的filterText变量传递给子product.component

app.component.html: app.component.html:

<app-product [filterText]="filterText"></app-product>

product.component.ts product.component.ts

@Input
filterText : string;

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

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