简体   繁体   English

Angular 2-使用@ContentChildren过滤组件的内容

[英]Angular 2 - Using @ContentChildren for filtering a component's content

I'm trying to implement a search component being able to trigger a search(term: string) function on specific children. 我正在尝试实现一个搜索组件,该组件能够在特定子search(term: string)上触发search(term: string)函数。 This search function will return null if the search on the child failed or a reference to the child if succeeded. 如果对子级的搜索失败,则此搜索功能将返回null;如果对子级的搜索成功,则此搜索函数将返回null。

This is so because I want to "collect" all references to the angular components that succeeded in a list, so that I could set them somehow as 'new' ContentChildren, so actually do basic filtering.. 之所以如此,是因为我想“收集”对列表中成功的角度分量的所有引用,以便可以将它们设置为“新的” ContentChildren,因此实际上可以进行基本过滤。

This is what I have so far: 这是我到目前为止的内容:

search.component.html search.component.html

<input (value)="searchterm" />
<ng-content>
    <!-- all components that implements iSearchable here -->
</ng-content>

search.component.ts search.component.ts

...
export class searchComponent implements OnChanges
{
    @Output('searchterm')
    public searchterm: string;

    // 1. Problem: ContentChildren does not work with interfaces?
    @ContentChildren(Isearchable)
    searchContent: QueryList<Isearchable>

    // 2. Problem: ngOnChanges will not fire when searchterm is changed
    ngOnChanges(event)
    {
        var toBeShown = [];

        // go through and let the components filter themselves
        for(let i = 0; i < this.searchContent.length; i++)
        {
            var element: Isearchable = this.searchContent[i];
            var result = element.search();

            if(result != null)  // search suceeded
                toBeShown.push(element);                
        }

        // 3. Problem: how to get them displayed ?
    }
}

Basicly I have 3 problems: 基本上我有3个问题:

1. The first is that @ContentChildren() won't accept the (yes, imported) interface Isearchable, since '[ts] Cannot find name Isearchable'. 1.首先是@ContentChildren()将不接受(是,已导入)Isearchable接口,因为“ [ts]找不到名称Isearchable”。 Alternatively all Isearchable components also have a common baseclass, but this also do not work. 另外,所有Isearchable组件也都具有一个公共基类,但这也不起作用。

Currently executing the program gives me the following error: "Uncaught (in promise): Unexpected directive value 'undefined' on the View of component 'SearchComponent'" 当前正在执行该程序会给我以下错误: “未捕获(按承诺):在组件'SearchComponent'的视图上出现意外的指令值'undefined'”

2. The second problem is that somehow the ngOnChanges won't be fired even if I type into the input. 2.第二个问题是,即使我在输入中键入内容,也不会以某种方式触发ngOnChanges。 This must be simple, do I miss something? 这一定很简单,我会错过吗?

3. Finally, the third problem is that I do not know exactly how to make the search results (that can be of different component types) being displayed in the DOM. 3.最后,第三个问题是我不知道如何使搜索结果(可以是不同的组件类型)显示在DOM中。 My hope is actually that I can somehow two-way bind to the @ContentChildren searchcontent. 我的希望实际上是我可以以某种方式双向绑定到@ContentChildren searchcontent。 This would probably solve the problem, wouldn't it? 这可能会解决问题,不是吗?

I really don't have a clue, how to continue, any help is appreciated! 我真的不知道如何继续,任何帮助都值得感谢! Thank you all! 谢谢你们!

Cheers 干杯

You can't use interfaces at this level since they only apply at design time not at runtime. 您不能在此级别使用接口,因为它们仅在设计时而不在运行时适用。

There is also an error in your code: 您的代码中还有一个错误:

@Input('searchterm') // instead of @Output
public searchterm: string;

To get the list of specified components as input, you need to define a base class for your component and use this class in @ContentChildren . 要获取指定组件的列表作为输入,您需要为组件定义一个基类,并在@ContentChildren使用该类。 An additional step is required to make this work. 需要额外的步骤才能完成这项工作。 You need to register the component itself into its providers against the parent class (with useExisting ). 您需要针对父类(使用useExisting )将组件本身注册到其提供程序中。 This explanation could appear a bit "abstract" but this question could help you and will give you a concrete sample: 该解释可能看起来有些“抽象”,但该问题可以为您提供帮助,并为您提供一个具体的示例:

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

相关问题 在Angular 2上使用* ngFor在多行中显示@ContentChildren中组件的内容 - Display component's content from @ContentChildren in multiple rows using *ngFor on Angular 2 如何在Angular 2中获得子组件的contentchildren? - How to get a child component's contentchildren in Angular 2? Angular 6:使用@ContentChildren和@HostBinding以编程方式显示/隐藏组件? - Angular 6: Show / Hide component programmatically using @ContentChildren and @HostBinding? 使用routerLink角6-7时组件未获得contentChildren - Component doesn't get contentChildren when using routerLink angular 6-7 Angular:使用@ContentChildren 将子组件放入另一个组件中 - Angular: Using @ContentChildren to get children inside another component 显示我使用 @ContentChildren 或 @ViewChild 获取的组件的内容 - Displaying content of Component that i fetched with @ContentChildren or @ViewChild Angular ContentChildren(Component)获得任何类型 - Angular ContentChildren(Component) get any type Angular 6 @ContentChildren不查询扩展组件 - Angular 6 @ContentChildren doesn't query extended component Angular 2单元测试组件,模拟ContentChildren - Angular 2 unit testing component, mocking ContentChildren Angular 9+:嵌套 ng-content 和 ContentChildren - Angular 9+: nested ng-content and ContentChildren
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM