简体   繁体   English

从按钮切换json值(单击)

[英]Toggle json value from button(click)

I'm new in JavaScript and Angular and I'm trying to get a json value from a single button(click) event. 我是JavaScript和Angular的新手,我试图通过单个按钮(单击)事件获取json值。

My code looks like this: 我的代码看起来像这样:

HTML code: HTML代码:

<button (click)="generatePdf(this.mockData)">Download PDF</button>

JSON code: JSON代码:

public mockData = [
{
      "_id": 1,
      "heading": {
        "department_id": 1,
        "address_info": "123 Street",
        "date": "Mar 20, 2020"
      },
      "greeting": "Greetings!",
      "body": "This is a sample text",
      "closing": "Sincerely,",
      "signature": "John Doe"
    },
    {
      "_id": 2,
      "heading": {
        "department_id": 2,
        "address_info": "456 Street",
        "date": "Feb 20, 2020"
      },
      "greeting": "Good Day",
      "body": "This is a sample text",
      "closing": "Sincerely,",
      "signature": "Sarah Jane"
    }]

Currently, I have a html file where all data is displayed from JSON value. 目前,我有一个html文件,其中所有数据都显示在JSON值中。 What I'm trying to do is get each json value and link it to each button in the html file. 我要做的是获取每个json值并将其链接到html文件中的每个按钮。 Any help would be greatly appreciated, Thanks!! 任何帮助将不胜感激,谢谢!

Try: 尝试:

<div *ngFor="let data of mockData ">
    <button (click)="generatePdf(data)">Download PDF for {{data.signature}}</button>
</div>

and in component code: 并在组件代码中:

generatePdf(data){
    console.log(data)
}

Note: Do not use this when working on HTML code. 注意:处理HTML代码时请勿使用this this is used to access variable that are within the scope of the component in ts files this用于访问ts文件中组件范围内的变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM