【问题标题】:Access element replaced with ng-content访问元素替换为 ng-content
【发布时间】:2021-08-13 23:13:28
【问题描述】:

我尝试创建一些选项卡组件,它们可以像 Angular Material 组件一样使用。

但我提出的解决方案不正确,而且有问题

我用过 document.getElementsByTagName('app-tab')

这是标签组件模板

这就是它的使用方式

我想访问app-tab标签属性值,但不知道如何从使用ng-content的app-tabs访问它 提前非常感谢你

答案

这称为内容投影

详细介绍可以看这里

https://blog.angular-university.io/angular-ng-content/

【问题讨论】:

    标签: angular typescript ng-template ng-content


    【解决方案1】:

    您可以使用 @ContentChild 或 @ContentChildren 装饰器来访问它。你可以在这里阅读它们:-

    https://angular.io/api/core/ContentChildren

    https://angular.io/api/core/ContentChild

    【讨论】:

    • 感谢您的早期回答,但这些技术需要指令和 ContentChildren 和 ContentChild 不能像那样工作
    • 指令不是必需的,您可以使用组件名称。或者给该组件一个带有哈希的标签,如#project,并在装饰器的圆括号中使用'project'。
    • 你是对的,我不知道,比你多
    猜你喜欢
    • 2022-08-02
    • 2020-05-12
    • 1970-01-01
    • 2018-02-27
    • 2017-08-28
    • 2017-12-05
    • 1970-01-01
    • 2016-08-01
    • 2013-12-12
    相关资源
    最近更新 更多