如何使用TypeScript为DOM元素创建自定义指令。

huangapple go评论65阅读模式
英文:

how to use a custom directive to a DOM element with typescript

问题

我在我的Angular项目中创建了一个自定义指令。我想将它用于TypeScript中的DOM元素。如果可能的话,如何做到这一点?

在我的搜索中,我没有找到任何有用的信息。感谢您!

英文:

I have created a custom directive in my angular project. I want to use it to a DOM element in typescript.
how to do that if it's possible?
thank you!

I didn't find anything useful in my searches

答案1

得分: 1

这是你要翻译的部分:

I think you're looking about how to make a custom directive & use to any HTML element to manipulate the tag also,

Here is some code snippet I hope it will help you to understand, Its an alternative way to make `*ngIf`. So far, this is the basic structure of making a custom directive you can take an Idea from it & extend it further. By the way, this directive is also type-safe.

import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';

class IfContext {
  public isShow: boolean = true;

  public get $implicit() {
    return this.isShow;
  }
}

@Directive({
  selector: '[appIf]',
  standalone: true
})
export class IfDirective {

  public ifContext = new IfContext()

  constructor(
    private viewContainerRef: ViewContainerRef,
    private templateRef: TemplateRef<any>
  ) {
  }

  @Input('appIf') set setIf(value: boolean) {
    this.ifContext.isShow = value;
    if (value) {
      this viewContainerRef.clear();
    } else {
      this viewContainerRef.createEmbeddedView(this.templateRef)
    }
  }

  static ngTemplateContextGuard(dir: IfDirective, ctx: unknown): ctx is IfContext {
    return true
  }

}

so, in the component you can use it like below & if you do not provide any boolean value it will throw an error.

<h2 *appIf="show">Hello World</h2>

Thanks
英文:

I think you're looking about how to make a custom directive & use to any HTML element to manipulate the tag also,

Here is some code snippet I hope it will help you to understand, Its a alternative way to make *ngIf so far this is the basic structure of making custom directive you can take Idea from it & extend it more further. By the way this directive also type-safe.

import {Directive, Input, TemplateRef, ViewContainerRef} from &#39;@angular/core&#39;;

class IfContext {
  public isShow: boolean = true;

  public get $implicit() {
    return this.isShow;
  }
}


@Directive({
  selector: &#39;[appIf]&#39;,
  standalone: true
})
export class IfDirective {

  public ifContext = new IfContext()

  constructor(
    private viewContainerRef: ViewContainerRef,
    private templateRef: TemplateRef&lt;any&gt;
  ) {
  }

  @Input(&#39;appIf&#39;) set setIf(value: boolean) {
    this.ifContext.isShow = value;
    if (value) {
      this.viewContainerRef.clear();
    } else {
      this.viewContainerRef.createEmbeddedView(this.templateRef)
    }
  }

  static ngTemplateContextGuard(dir: IfDirective, ctx: unknown): ctx is IfContext {
    return true
  }

}

so, in the component you can use it like bellow & if you not provide any boolean value it will throw error.

    &lt;h2 *appIf=&quot;show&quot;&gt;Hello World&lt;/h2&gt;

Thanks

答案2

得分: -1

我建议您在一个[共享模块][1]中声明您的指令,并将该指令导出以在您创建的整个项目中使用。
像这样:

    @NgModule({
      declarations: [customDirective],
      exports:[customDirective]
    })
    export class SharedModule { }

  [1]: https://angular.io/guide/sharing-ngmodules
英文:

I would recommend you declare your directive in a shared module and export the directive to use it over the complete project you create.
like this:

@NgModule({
  declarations: [customDirective],
  exports:[customDirective]
})
export class SharedModule { }

huangapple
  • 本文由 发表于 2023年4月10日 22:29:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定