动态安装 angular 2 指令

编程入门 行业动态 更新时间:2024-10-25 11:32:45
本文介绍了动态安装 angular 2 指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

上下文

我有一个简单的指令,可以根据收到的属性向给定的 HTML 元素添加一些属性.

<button class="btn btn-blue x-large" [myDirective]="{ some_json_data: true }">展开

myDirective 指令只是在 ngOnInit 钩子中执行一些逻辑并装饰 ElementRef 原生元素(在本例中为按钮)添加属性,没什么复杂的.

ngOnInit(): void {const el: 元素 = this.element.nativeElement;this.decorate(el, this.myDirective);}

问题

基于给定的逻辑(在 myDirective 装饰中),我想在 myDirective 处向 ElementRef 引用的元素添加工具提示(这是另一个指令).

如何手动挂载指令以及如何将其添加到元素 (ViewContainerRef)?

解决方案

您不会动态添加或删除指令,但如果您想添加工具提示您必须在组件模板中插入一个 div 以根据变量状态激活它

<div [hidden]="tooltipnotshown"类=工具提示">等等等等……

Context

I have a simple directive that add some attributes to a given HTML element based on received attributes.

<button class="btn btn-blue x-large" [myDirective]="{ some_json_data: true }">
    Unfold
</button>

The myDirective directive just does some logic in the ngOnInit hook and decorates the ElementRef native element (in this case the button) adding attributes, nothing complicated.

ngOnInit(): void {
  const el: Element = this.element.nativeElement;
  this.decorate(el, this.myDirective);
}

Problem

Based on a given logic (in myDirective decoration) I want to add a tooltip (which is another directive) to the element referenced by ElementRef at myDirective.

How do I mount a directive manually and how do I add it to an element (ViewContainerRef)?

解决方案

You do not add or remove directives dynamically, but if you want to add a tooltip you have to insert a div in your component template that activates it depending on a variable status

<div [hidden]="tooltipnotshown"
     class="tooltip">
 blah blah or whatever ...
</div>

这篇关于动态安装 angular 2 指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 23:08:24,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指令   动态   angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!