问题描述
我具有以下DOM结构
<div #carousalElement>
<div class="singleCaousalElement">
<a>
<img [src]="carousalURL1" class="carousalImage">
</a>
</div>
<div class="singleCaousalElement">
<a>
<img [src]="carousalURL2" class="carousalImage">
</a>
</div>
</div>
我可以使用ViewChildren
@ViewChildren('carousalElement') carousalElements;
问题::当我在#carousalElement
div下动态添加新的div
时,它没有显示在carousalElements
数组下.
PROBLEM: When I dynamically add a new div
under the #carousalElement
div, it does not show up under the carousalElements
array.
我缺少明显的东西吗?
推荐答案
Angular无法识别Angular指令或API未添加的HTML.同样,如果<div>
没有模板变量#carousalElement
,则@ViewChildren('carousalElement')
也将找不到它.
Angular doesn't recognize HTML not added by Angular directives or APIs. Also if the <div>
doesn't have a template variable #carousalElement
, @ViewChildren('carousalElement')
won't find it.
模板变量需要静态添加到组件模板中.动态添加它们是没有意义的. Angular处理仅在编译模板期间特定于Angular的模板构造.
Template variables need to be added statically in the components template. Adding them dynamically is meaningless. Angular processes Angular-specific template constructs only during compilation of a template.
这篇关于@ViewChildren不会使用动态添加的DOM元素进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!