简体   繁体   English

angular 如何使用查询列表对象?

[英]How querylist object is used by angular?

I am learning angular and came across view children and content children.我正在学习 angular 并遇到了视图儿童和内容儿童。 These are querlist objects in angular.这些是 angular 中的 querlist 对象。 During lifecycle hook, angular updates querlist.在生命周期钩子期间,angular 更新 querlist。 and query list object stores view nodes.和查询列表对象存储视图节点。 This all is theoretical.I want to ask what is mean by update the query list or making a query?这都是理论上的。我想问一下,更新查询列表或进行查询是什么意思? What is a query in the query list?什么是查询列表中的查询?

The query is the directive that you pass into the ViewChildren decorator.查询是您传递给 ViewChildren 装饰器的指令。

A directive in Angular is something that adds some extra behavior to the HTML elements or they modify the DOM. Angular 中的指令是向 HTML 元素添加一些额外行为或修改 DOM 的东西。 A Component is also a directive which has a view/template associated with them.组件也是一个指令,它有一个与之关联的视图/模板。

eg例如

@ViewChildren(ChildDirective) viewChildren !: QueryList<ChildDirective>;

So essentially it will list all the directives that match 'ChildDirective' currently present in the DOM.所以本质上它会列出与当前存在于 DOM 中的“ChildDirective”匹配的所有指令。 It searches for the provided directive/component in the DOM, hence it is referred to as Query.它在 DOM 中搜索提供的指令/组件,因此它被称为查询。 Children - a plural which suggests you expect more than one matching the criteria and hence you need a list to hold them.儿童 - 复数形式,表示您期望符合条件的不止一个,因此您需要一个列表来保存它们。

Angular has provided inbuilt Interfaces/Types to deal with it, QueryList. Angular 提供了内置的接口/类型来处理它,QueryList。 Any time a child component/directive is added, removed, or moved, the query list will be updated任何时候添加、删除或移动子组件/指令,查询列表都会更新

Here is an excellent article that will give a more clear idea.这是一篇出色的文章,可以提供更清晰的想法。 https://blog.mgechev.com/2016/01/23/angular2-viewchildren-contentchildren-difference-viewproviders/ https://blog.mgechev.com/2016/01/23/angular2-viewchildren-contentchildren-difference-viewproviders/

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

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