繁体   English   中英

用于构建动态 HTML 的最合适的 Angular 组件/指令/管道是什么?

[英]What is the most appropriate Angular component/directive/pipe to use to build dynamic HTML like this?

(我从 React 来到 Angular,所以如果它看起来以 React 为中心,请原谅我的问题。)

我需要创建一个 Angular 组件,该组件接受一个字符串和该字符串中的术语列表以显示工具提示。

const sentence = "The fox jumped over the moon.";

const tooltips = {
  "text": "jumped over",
  "tooltip": "Blah blah blah."
}, {
  "text": "moon",
  "tooltip" "Object seen in the sky at night."
};

output 预计如下所示:

<div>The fox <span onclick="showTooltip(0)">jumped over</span> the <span onclick="showTooltip(1)">moon</span>.</div>
<div id="tooltip_0">Blah blah blah</div>    
<div id="tooltip_1">Object seen in the sky at night.</div>    

用于创建此动态结构的最合适的 Angular 组件/指令/管道是什么?

在 Angular 中,您可以使用@Input()从父组件获取值。

在您的child.ts中,您将拥有:

@Input() textInput: string;

在您的parent.html ,您将拥有:

<app-child-component [textInput]="variableText"></app-child-component>

然后,您将能够将child.ts中的textInput用作任何其他变量。

更多详细信息: https://angular.io/guide/component-interaction

暂无
暂无

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

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