如何在angular 6中创建元素指令?我看到了如何创建一个属性指令,但是如果我想创建一个自定义元素呢?
@Directive({
selector: '[appCards]'//you can't do something like -- element: name
})
export class CardsDirective {
constructor(el: ElementRef) {
}
}
发布于 2018-10-25 11:31:17
我试着用Selector as创建一个指令,它起作用了。Angular是如此强大。
import {Directive, ElementRef, Input} from '@angular/core';
@Directive({
selector: '<appCards>'
})
export class CardsDirective {
@Input() label;
constructor(el: ElementRef) {
console.log('it called');
}
ngOnInit(){
console.log(this.label);
}
}
模板:
<appCards label="change"></appCards>
发布于 2019-12-10 16:16:54
在angular中,您可以通过以下方式创建element指令
@Directive({
selector: 'appCards'
})
export class CardsDirective {
constructor(el: ElementRef) {
}
}
您只需删除选择器中的[]括号,即可使其成为element指令。
发布于 2021-03-12 21:40:31
根据Angular文档,您可以通过以下方式之一声明选择器。因此,如果要使用指令创建元素,则必须将指令的选择器声明为
选择器:'elementName'
https://stackoverflow.com/questions/52980832
复制相似问题