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

时间:2019-01-14 06:44:42

标签: angular ionic-framework angular5 angular6

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

模板

<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);

  }

4 个答案:

答案 0 :(得分:0)

您可以像

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

答案 1 :(得分:0)

值属性用于输入,在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);

  }

答案 2 :(得分:0)

如果要在单击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);
}

答案 3 :(得分:0)

请尝试

<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 ***');
}
相关问题