Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 从指令获取nativeElement_Angular - Fatal编程技术网

Angular 从指令获取nativeElement

Angular 从指令获取nativeElement,angular,Angular,我尝试使用指令获取nativeElement。 这是我的指示: import { Directive, ElementRef } from "@angular/core"; ... @Directive({ selector: '[someData]', exportAs: 'someData' }) export class MyDirective { constructor(private elRef: ElementRef) { } } 我的模板如下

我尝试使用指令获取nativeElement。 这是我的指示:

import { Directive, ElementRef } from "@angular/core";
...

@Directive({
    selector: '[someData]',
    exportAs: 'someData'
})

export class MyDirective {
    constructor(private elRef: ElementRef) {

    }
}
我的模板如下所示:

<div *ngFor="let note of notes;let index;">
 ...
 <div someData #text="someData">
   ...
 </div>
 <button (click)="showNative()">click me </button>
</div>
当我单击第一个按钮或第二个按钮时,我在控制台中只看到一个对象,它是第一个nativeElement,但我需要如下内容:

@ViewChildren(MyDirective) children: QueryList(MyDirective);
单击第一个按钮-我想看到第一个对象 单击第二个按钮-我想看到第二个对象 等等

我知道我必须在func“showNative”中传递ID或链接,
但是我不知道如何…

如果您只是想获取指令的元素,只需将本地模板引用传递给
showNative
调用:

<div someData #text>
...
button (click)="showNative(text)">click me </button>

showNative(element): void {
   console.log( element );
}
@ViewChildren(MyDirective) children: QueryList(MyDirective);