【问题标题】:Want to understand the working of @ViewChild()想了解@ViewChild() 的工作原理
【发布时间】:2019-09-03 18:10:55
【问题描述】:

我的问题 ID 与之前的主题答案相关。 Angular 7 Hide bootstrap modal in typscript

提供的在打字稿代码中隐藏模式的解决方案是让 @ViewChild(ModalDirective) 模式:ModalDirective 访问本地引用,然后在其上使用方法隐藏。

但根据我作为 Angular 初学者的理解 @ViewChild('modal') modal :应该使用 ModalDirective,因为 @ViewChild 将本地引用作为参数。 那我们为什么要通过 ModalDirective。

【问题讨论】:

    标签: angular


    【解决方案1】:

    来自文档:https://angular.io/api/core/ViewChild

    支持以下选择器。

    • 任何带有@Component@Directive 装饰器的类

    • 作为字符串的模板引用变量(例如查询<my-component #cmp></my-component>@ViewChild('cmp')

    • 在当前组件的子组件树中定义的任何提供程序(例如 @ViewChild(SomeService) someService: SomeService)
    • 通过字符串标记定义的任何提供者(例如@ViewChild('someToken') someTokenVal: any
    • 一个TemplateRef(例如用@ViewChild(TemplateRef) template;查询<ng-template></ng-template>

    【讨论】:

    • 在父组件中获取子组件提供者的用例是什么。例如,在当前组件的子组件树中定义的任何提供者(例如@ViewChild(SomeService) someService: SomeService)
    猜你喜欢
    • 2016-02-20
    • 2012-12-11
    • 2011-02-18
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多