【问题标题】:Angular- Hide button till certain conditions are metAngular-隐藏按钮,直到满足某些条件
【发布时间】:2021-11-09 16:03:02
【问题描述】:

我制作了一个反应式表单,用户可以在其中添加数据。在那里我添加了一个add 按钮和一个remove 按钮。单击add 按钮时,用户可以添加更多输入字段并在单击remove 时删除这些字段。现在,如果只有一个输入字段,我想隐藏 remove 按钮。我该怎么做? HTML 代码:

<div class="row">
  <div class="col">
    <div class="row ml-0" style="display: flex;">
      <span>Add custom questions:</span>
      <mat-checkbox
        style="margin-left: 10px! important;"
        (change)="selectCheck()"
      >
      </mat-checkbox>
    </div>
    <div *ngIf="checked">
      <span style="font-style: italic; font-size: 9px;"
        >NOTE : Panel members can respond with text based answers, which will be
        visible to the winner selector</span
      >
      <form [formGroup]="userForm">
        <div
          class="row"
          *ngFor="let question of getQuestionFormControls(); let i = index"
        >
          <span class="ml-3 pt-2">Question {{ i + 1 }}</span>
          <input
            class="form-control ml-3 mt-1"
            [formControl]="questions"
            type="text"
          />
        </div>
        <div
          class="col mt-1 justify-content-between"
          style="display:flex;align-items:center;"
        >
          <!-- <i class="bi bi-plus-square"></i> -->
          <div class="d-flex mt-1">
            <mat-icon
              style="color: #5663ED;cursor:pointer;margin-top: 0rem;"
              (click)="addQuestion()"
              >add_box</mat-icon
            >
            <span>Add more questions</span>
          </div>
          <div class="d-flex mt-1">
            <mat-icon
              (click)="removeQuestion(i)"
              style="color: #5663ED;cursor:pointer"
              >remove_circle</mat-icon
            >
            <span>Remove</span>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>

ts 代码:

checked = false;
userForm: FormGroup;

  constructor(private fb: FormBuilder) {
    this.userForm = new FormGroup({
      questions: this.fb.array([this.fb.control(null)]),
    });
  }

  addQuestion(): void {
    (this.userForm.get('questions') as FormArray).push(this.fb.control(null));
  }

  removeQuestion(index) {
    (this.userForm.get('questions') as FormArray).removeAt(index);
  }

  getQuestionFormControls(): AbstractControl[] {
    return (<FormArray>this.userForm.get('questions')).controls;
  }

  selectCheck() {
    this.checked = !this.checked;
  }

我尝试使用*ngif="i &gt;= 1" 并希望使用索引,但没有成功。我该怎么办?

project 的演示

【问题讨论】:

  • ngIf 是正确的工具;确保您将“if”中的“i”大写。
  • *ngIf="getQuestionFormControls().length >= 1"
  • @Icycool 我试过这个并且它有效。但是在这里,删除按钮仅在最后一个输入字段之后可见。如何使其在每个输入字段中都可见?检查问题中的链接以了解我想说的内容
  • 在这种情况下,您需要将删除按钮放在 ngFor 中
  • @Icycool 是的,谢谢。我在这里遇到了另一个问题。我试图保持禁用添加按钮(但仍然可见),直到用户输入一些数据。我该怎么做?我尝试使用ngIf,但它只会隐藏按钮,直到用户单击输入字段。 The link

标签: angular angular-reactive-forms angular-ng-if angular10


【解决方案1】:

我没有测试过,但我想这可以正常工作:

<div 
   class="d-flex mt-1"
   *ngIf="(this.userForm.get('questions'))?.length > 1"
>
            <mat-icon
              (click)="removeQuestion(i)"
              style="color: #5663ED;cursor:pointer"
              >remove_circle</mat-icon
            >
            <span>Remove</span>
</div>

如果不起作用,请尝试更改此行:

*ngIf="(this.userForm.get('questions'))?.controls?.length > 1"

【讨论】:

  • Identifier 'length' is not defined. 'AbstractControl' does not contain such a member 收到此错误
  • *ngIf="(this.userForm.get('questions'))?.length > 1" 需要像这样访问表单控件的值 *ngIf="this.userForm.get( '问题')?.value?.length > 1"
猜你喜欢
  • 2014-02-03
  • 2018-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
相关资源
最近更新 更多