简体   繁体   English

angular2中的多个复选框值

[英]Multiple Checkbox value in angular2

I have one user model and one role model. 我有一个用户模型和一个角色模型。 I want to send value of role into db using checkbox in angular2 in http put request. 我想使用http put请求中angular2中的复选框将角色的值发送到db中。 I have one table user_role and I want to send role_id into that table. 我有一个表user_role,我想将role_id发送到该表中。 For the I am fetching all roles and showing it in html like this: 对于我来说,我正在获取所有角色,并在html中显示如下:

 <a *ngFor="let role of roles">
   <input type="checkbox" [value]="role.name" [(ngModel)]="user.role_id" [ngModelOptions]="{standalone: true}"/>&nbsp;<b>{{role.name}}</b>
 </a>

Now I want, if I will check multiple checkboxes the multiple values should go into database. 现在我想要,如果我将选中多个复选框,则多个值应进入数据库。 I am new in angular2. 我是angular2的新手。 Could anyone please help me in getting this? 谁能帮我得到这个吗?

I have created a sample demo where you can understand how multiple checkbox value can be sent to server 我创建了一个示例演示,您可以在其中了解如何将多个复选框值发送到服务器

here is my .ts file code 这是我的.ts文件代码

roles: Array<String> = [
{
  "name":"qa",
  "isSelected": false
},
{
  "name":"developer",
  "isSelected": false
},
{
  "name":"manager",
  "isSelected": false
},
{
  "name":"BA",
  "isSelected": false
},];
sendDataToServer() {
alert(JSON.stringify(this.roles)); }

I have a json roles and a function sendDataToServer that displays the final json that is to be sent on server 我有一个json 角色和一个函数sendDataToServer ,该函数显示将在服务器上发送的最终json

here is my .html code 这是我的.html代码

 <a *ngFor="let role of roles">
 <input type="checkbox" [value]="role.name" [(ngModel)]="role.isSelected"        [ngModelOptions]="{standalone: true}"/>&nbsp;<b>{{role.name}}</b>
 </a>
<br><br>
<input type="button" value="Send Data On Server "    (click)="sendDataToServer()"/>

So I have 4 checkbox and I have bind these checkbox with isSelcted property of json. 所以我有4个复选框,并将这些复选框与json的isSelcted属性绑定。 As soon as user click on checkbox the value changes false to true. 用户单击复选框后,该值将false更改为true。

when user click on sendDataToServer button then we have the updated isSelected values. 当用户单击sendDataToServer按钮时,我们具有更新的 isSelected值。

Hope this will help you. 希望这会帮助你。 :) :)

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

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