如何获取模板中标签属性的值

how to get value of the attribute of tag in template

如何获取模板中tag属性的并存入 打字稿中的变量。 为此,我尝试使用下面的代码,但出现错误

模板

<div *ngFor="let item of store">
<div (click)='getMatchid("value")'   value={{item.ttypeUid}} >{{item.ttypeName}}</div>
              </div>

typecsript

  getMatchid(val){
    this.MatchId = val
    console.log('val ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ' + val);

  }

你可以像

一样直接将ttypeUid传给getMachid
<div (click)='getMatchid(item.ttypeUid)'>{{item.ttypeName})</div>

值属性用于输入,在div

中不可用

像这样将 Id 作为参数传递给函数本身,

HTML

<div *ngFor="let item of store">
    <div (click)='getMatchid(item.ttypeUi)'>
         {{item.ttypeName}}
    </div>  
</div>

TS

 getMatchid(val){
    this.MatchId = val
    console.log('val ++++++++++++++++++++++++++ ' + val);

  }

如果你想在点击 div 时获取 ttypeUid 那么 你可以这样写

HTML代码

   <div *ngFor="let item of store">
     <div (click)='getMatchid(item.ttypeUid)'>{{item.ttypeName}}</div>
   </div>

TS代码

getMatchid(val){
  // you will get here, val == item.ttypeUid
  this.MatchId = val
   console.log('val ++++' + val);
}

请试试这个

<div *ngFor="let item of store">
    <div (click)="getMatchid(item.ttypeUid)">{{item.ttypeName}}</div>
</div>



getMatchid(val){
    this.MatchId = val;
    console.log(val, '*** Value From Div Click ***');
}