【问题标题】:How to change the atributte of an object in ngFor?如何更改 ngFor 中对象的属性?
【发布时间】:2019-11-06 23:16:25
【问题描述】:

我开始学习angular,遇到了以下情况:

<div class="container mt-5 mb-5">
    <h3>What is the disease?</h3>
    <div class="container" >
        <div class='col-2' *ngFor="let disease of diseases;let i= index"> 
              <div class="form-check">
                  <input type="checkbox" class="form-check-input"  [(ngModel)]="paciente.disease" name="inlinecheckbox{{i}}" id="inlinecheckbox{{i}}" >
                  <label class="form-check-label" for="inlinecheckbox{{i}}">{{disease}}</label>
                </div>
        </div>
       <p *ngIf="pacient.disease==true && disease=='fever'">
          <input type="radio" name="high">High Fever
          <input type="radio" name="Average">Average Fever
          <input type="radio" name="low">Low Fever
       </p>
    </div>
  </div>

基本上,我正在为每种疾病尝试checkbox,每次单击时,我都会更改患者相应属性中的值并打开另一个对话框树。

这是类的控件:

patient:Patient; 
diseases=["fever","faintness","tiredness"];

这就是模型:

export class Patient{
  fever:boolean
  faintness:boolean
  tiredness:boolean
}

正如我现在所做的那样,每次我点击一个选项时都会被标记。如何解决,是否可以按照我的方式解决?

【问题讨论】:

  • paciente 变量里面是什么
  • 你在patient没有什么叫疾病
  • 对不起,pacient 是对患者的未翻译词。我现在调整了
  • 检查其他评论patient.disease未定义
  • 是的,这就是问题所在。我正在尝试使用 ngfor 更改属性以通过每种疾病

标签: angular typescript angular-directive


【解决方案1】:

我会试试这个:

<div class="container mt-5 mb-5">
    <h3>What is the disease?</h3>
    <div class="container" >
        <div class='col-2' *ngFor="let disease of diseases;let i= index"> 
              <div class="form-check">
                  <input type="checkbox" class="form-check-input"  [(ngModel)]="patient[disease]" name="inlinecheckbox{{i}}" id="inlinecheckbox{{i}}" >
                  <label class="form-check-label" for="inlinecheckbox{{i}}">{{disease}}</label>
                </div>
        </div>
       <p *ngIf="patient[disease]==true && disease=='fever'">
          <input type="radio" name="high">High Fever
          <input type="radio" name="Average">Average Fever
          <input type="radio" name="low">Low Fever
       </p>
    </div>
  </div>

在你的类中没有 diseaseattribute 这样的东西,你可能想做patient[disease]

例如,如果diseases 中的disease 等于'fever',那么您对布尔变量的调用将是patient['fever'],它与patient[disease] 相同,与patient.fever 相同

【讨论】:

    猜你喜欢
    • 2021-12-25
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多