简体   繁体   English

如何使用动态数据创建动态按钮并在按钮单击角度时获得动态值

[英]How to create a dynamic button using dynamic data and get dynamic values on the button click in angular

Here i am getting below Dynamic data from server 在这里,我从服务器获取以下动态数据

{
    "data": [
        {
            "id": 4,
            "first_name": "Eve",
            "last_name": "Holt",
            "lat":"25.6599899",
            "lng":"45.3664646",
            "status":"0"

        },
        {
            "id": 5,
            "first_name": "Charles",
            "last_name": "Morris",
            "lat":"25.99899",
            "lng":"45.4646",
             "status":"1"

        },
        {
            "id": 6,
            "first_name": "Tracey",
            "last_name": "Ramos",
            "lat":"25.2339899",
            "lng":"45.56664646",
            "status":"1"

        }
    ]
}

Here how to create the Dynamic buttons based on the Status value suppose if status=1 value having 3 members then 3 buttons have to be created with that particular names and ids and when the user clicks any button that particular of the person and name has to be displayed on alert 在这里,如何基于状态值创建动态按钮,假设如果status = 1值具有3个成员,则必须使用该特定名称和ID创建3个按钮,并且当用户单击该人员和姓名的特定按钮时,警报显示

As is understand like you need button for each status = 1 and on click need to display name of person. 众所周知,您需要每个状态= 1的按钮,并且在单击时需要显示人员名称。

ts code ts代码

Variable 变量

public dynamicData = {
"data": [
  {
    "id": 4,
    "first_name": "Eve",
    "last_name": "Holt",
    "lat": "25.6599899",
    "lng": "45.3664646",
    "status": "0"

  },
  {
    "id": 5,
    "first_name": "Charles",
    "last_name": "Morris",
    "lat": "25.99899",
    "lng": "45.4646",
    "status": "1"

  },
  {
    "id": 6,
    "first_name": "Tracey",
    "last_name": "Ramos",
    "lat": "25.2339899",
    "lng": "45.56664646",
    "status": "1"

  }
]
};

Method 方法

onButtonClick(data: any): void {
   alert(data.first_name + ' status is ' + data.status);
}

HTML Code HTML代码

  <ng-container *ngFor="let data of dynamicData.data">
     <button [id]="data.id" class="btn btn-primary" (click)="onButtonClick(data)">
        {{data.first_name}}
     </button>
  </ng-container>

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

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