Angular:从外部访问 ng-content 嵌套组件方法

2024-01-16

我有两个共享组件,我希望父组件调用其子组件中的方法

共享组件 1(父组件)

@Component({
selector: 'parent',
template: `<div>
                <div #parentBody>
                   <ng-content select="[parentBody]"></ng-content>
                </div>
                <button (click)=" " >tell child to dance</button>
            </div>
        `,
})
export class ParentComponent {
  constructor() { }

}

共享组件 2(子组件)

@Component({
selector: 'child',
template: `<div>
                <p>I'm a child component</p>
           </div>
          `,
})
export class ChildComponent {
  dance() {
     alert('dancing');
  }
}

并在应用程序组件中

<parent>
    <div parentBody>
       <child></child>
    </div>
<parent>

在这种情况下,我们如何在父组件和子组件之间进行通信


您可以通过两种方式做到这一点:

1) @Output父组件中的参数。

父组件.ts:

import { Component, Input, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'parent',
  template: `
    <div>
      <div #parentBody>
        <ng-content select="[parentBody]"></ng-content>
      </div>
      <button (click)="dance.emit()">tell child to dance</button>
    </div>
  `
})
export class ParentComponent {
  @Output() dance = new EventEmitter();
}

应用程序组件.html:

<parent (dance)="myChild.dance()">
    <div parentBody>
       <child #myChild></child>
    </div>
<parent>

检查这个简单的 StackBlitz 演示 https://stackblitz.com/edit/angular-ng-content-so-54304318这是使用 @Output 方法


2) @ContentChild父组件中的参数。

(在@trichetriche评论此问题后添加。感谢@trichetriche!)

父组件.ts:

import { Component, Input, ContentChild } from '@angular/core';

import { IChild } from './i-child';

@Component({
  selector: 'parent',
  template: `
    <div>
      <div #parentBody>
        <ng-content select="[parentBody]"></ng-content>
      </div>
      <button (click)="onClick()">tell child to dance</button>
    </div>
  `
})
export class ParentComponent {
  @ContentChild('myChild') child: IChild;

  onClick() {
    this.child.dance();
  }
}

应用程序组件.html:

<parent>
    <div parentBody>
       <child #myChild></child>
    </div>
<parent>

检查这个简单的 StackBlitz 演示 https://stackblitz.com/edit/angular-ng-content-contentchild-so-54304318这是使用 @ContentChild 方法


无论哪种方式都可以很好地满足您的要求。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular:从外部访问 ng-content 嵌套组件方法 的相关文章

随机推荐

  • `more.com` 返回“内存不足”。

    环境详情 x64 Win7 SP1 企业版 Windows PowerShell v5 0 没有加载任何配置文件 我的本地电源外壳 questions tagged powershell会话正在返回 内存不足 当我尝试执行时help or
  • 如何对随时间缓慢变化的数据进行建模?

    假设我得到了大量 200 万行 数据 这些数据应该是静态且不变的 应该是 这些数据每月重新发布一次 有哪些方法可以用于 1 了解哪些数据点逐月发生变化以及 2 使用给定时间点的数据 解决方案1 天真地保存每个数据快照 并按日期注释 差异意识
  • 在从独立相机捕获的图像上绘制文本(时间戳)

    我的代码如下 单击即可打开相机 拍照 从相机获取照片 然后放入图像视图中 不过 我想拍摄图像并在图像上应用文本 某种时间戳 最好是图像的时间戳 或者只是系统日期时间 并保存为 jpeg 如果有人能帮助我那就太好了 public class
  • 如何存储网络应用程序的配置设置?

    我有一些站点元数据我希望可以更改 例如 在我的应用程序中 如果系统管理员不想使用站点的 库存 部分 他 她可以将其关闭 并且它将从主站点消失 所以我在想 也许我可以在数据库中创建一个名为 元 的表 并在那里插入值 或元组 然后 如果模块被关
  • 将“@daily-co/daily-js”导入 SvelteKit 应用程序会引发“全局未定义”错误

    我尝试过的 我尝试通过解决它if browser 进一步来说 if browser let DailyIframe await import daily co daily js 在load函数里面
  • 如何读写MP3到数据库

    如何从Sql数据库读取MP3 在 sql 中 我已将文件存储为二进制格式 现在我想检索存储在 sql 中的 Mp3 文件并显示在我的 aspx 页面中 如何 请帮忙 以最简单的形式 这就是您获取原始字节的方式 在不知道您想要它做什么的情况下
  • 分段错误:11 - Xcode 6.3

    无法存档 我的应用程序在模拟器和多个设备上运行良好 Xcode 6 3 2 基于 swift 但是当我尝试存档它时出现错误Command failed due to signal Segmentation fault 11 其他人面临同样的
  • 视图索引 (Oracle)

    假设我有两张桌子 tab a and tab b 我创建了一个如下所示的视图 create view join tabs as select col x as col z from tab a union select col y as c
  • Objective C 中什么是非空?

    有人可以详细说明为什么吗nonnulliOS 9 中引入 例如 NSArray method instancetype array is now instancetype nonnull array 参考 https developer a
  • C 的 std::vector 替代品 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我想知道是否有替代方案C 中的 std 向量 我发现这个实现 http codingrecipes com implementation
  • (Flutter) 具有有限内容的无限滚动 `ListView.builder`

    1 问题 我该如何做我的ListView builder能够滚动到顶部和底部的空白区域吗 例如 我有一个自定义小部件列表 我希望用户能够通过滚动到列表中最上面的卡片 位于屏幕顶部 更接近他的拇指 而Flutter用空背景渲染顶部空间 2 到
  • 使用 MVC Web API 发布对象数组

    我有一个基本的后期操作 适用于单个对象RecordIem 我想做的是执行相同的操作 但通过使用相同格式发布一组请求来批量执行 例如 public HttpResponseMessage Post RecordItem request var
  • Python Multiprocessing.Pool 惰性迭代

    我想知道 python 的 Multiprocessing Pool 类与 map imap 和 map async 一起使用的方式 我的特殊问题是 我想映射一个创建内存密集型对象的迭代器 并且不希望所有这些对象同时生成到内存中 我想看看各
  • 用于查找时间跨度的最大平均值的 LINQ 查询

    我有一组数据有两个点 瓦特 和时间戳 每个数据点间隔 1 秒 所以它看起来像这样 0 01 100 0 02 110 0 03 133 0 04 280 该数据集长达几个小时 我想编写一个查询 在其中可以找到不同时间段 5 秒 1 分钟 5
  • 如何在 Android Retrofit 中忽略 JSON 元素

    我正在开发一个 Android 应用程序 它使用 Android Retrofit 发送 JSON 它将 POJO 类转换为 JSON 它工作正常 但我需要在发送 JSON 时忽略 POJO 类中的一个元素 有谁知道 Android Ret
  • 在 linux ubuntu 中使用 CPAN 时,我应该使用 sudo / 作为 root 或作为我的默认用户运行它

    我收到这样的错误 Running make install Prepending blib arch and blib lib of 17 build dirs to PERL5LIB for install ERROR Can t cre
  • 将 Markdown 转换为 HTML 的正则表达式

    如何编写正则表达式将 mark 转换为 HTML 例如 您可以输入以下内容 This would be italicized text and this would be bold text 然后需要将其转换为 This would be
  • TomEE 8 服务器仅限于 Netbeans 15 中的 Java EE7 Web 应用程序

    所以我从这里安装了最新的 Apache TomEE 8 服务器 https tomee apache org download html https tomee apache org download html TomEE 服务器运行良好
  • 如何使用 OpenCV 和 Python 使用遮罩(或透明度)在图像中查找模板?

    假设我们正在寻找这个模板 我们模板的角是透明的 因此背景会有所不同 如下所示 假设我们可以在模板中使用以下掩码 找到它会很容易 我尝试过的 我努力了matchTemplate但它不支持蒙版 据我所知 并且在模板中使用 alpha 通道 透明
  • Angular:从外部访问 ng-content 嵌套组件方法

    我有两个共享组件 我希望父组件调用其子组件中的方法 共享组件 1 父组件 Component selector parent template div div div div