簡體   English   中英

在 html 文件中按 id 查找元素

[英]Find an element by its id in the html file

我有一個數組

items = [{
"id" : 1,
"name": "peter"},
{
"id" : 2,
"name": "john"}
    ...
]

我試圖在 html 文件中找到一個基於 id 的項目,我試圖隱藏和元素。

可以在 js 文件中輕松完成,但我想看看在 html 文件中是否有簡單的方法

您可以將模板選擇器添加到元素

<input #myElement>

然后你可以在你的ts文件中添加一個引用

@ViewChild('myElement') myInput: ElementRef<HtmlInputElement>;

甚至直接在模板中引用元素

<button (click)="myElement.focus()"></button>

如果你想隱藏一個項目,假設它的索引是 2,你可以在 HTML 中做這樣的事情

<ng-container *ngFor="let eachItem of allArrayItems; let index=i">
  <div *ngIf="i != 2">  // This ngIf will be hidden if the index is 2
     whatever logic
  </div>
</ng-container>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM