【问题标题】:How to make checked and unchecked a checkbox using true and false value of json in angular 7 [duplicate]如何使用角度7中json的真假值来选中和取消选中复选框[重复]
【发布时间】:2020-02-09 07:35:25
【问题描述】:

我有一个表,其数据来自角度 7 中的循环。这里一切正常,但根据我的 json,列中有 "enable":true 和 "enable":false。如果我的值是应选中 true 复选框,如果值为 false 则应在表中取消选中复选框。这是我的代码如下

app.component.html

<table>
<tr *ngFor="let user of users">
<td>{{user.id}}</td>
<td>{{user.name}}</td>
<td><input type="checkbox" checked></td>
</tr>
</table>

app.component.ts

import { Component, OnInit } from '@angular/core';
import { CommonserviceService } from './../utilities/services/commonservice.service';
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
  getListData: any;

  constructor(private commonserviceService: CommonserviceService) { }

  ngOnInit() {


  }

  users = [{"id":1,"name":"test","enable":true},{"id":2,"name":"test","enable":false}];


}

【问题讨论】:

标签: javascript html angular


【解决方案1】:

试试这个:

<table>
    <tr *ngFor="let user of users">
        <td>{{user.id}}</td>
        <td>{{user.name}}</td>
        <td><input type="checkbox" [checked]="user.enable"/></td>
    </tr>
</table>

【讨论】:

    猜你喜欢
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 2018-10-27
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    相关资源
    最近更新 更多