英文:
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 '@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 bellow & if you not provide any boolean value it will throw error.
<h2 *appIf="show">Hello World</h2>
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 { }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论