javascript - 检查单选按钮是否脏/以 react 形式被触摸

标签 javascript angular typescript angular-reactive-forms

My StackBlitz example

我已经构建了一个 Angular Reactive Form。在它上面我有 2 个单选按钮,默认选择一个(但是在我的示例中它没有在 UI 上显示为选中)。

我有一个 ngIf div,如果单选按钮已更改或触摸 || 则应显示它脏,例如

<div formGroupName="radioButtons" class="form-group col-6 pl-0 pt-3">
    <div class="form-check-inline">
        <label for="yes" class="col-12 customradio"
            ><span>yes</span>
            <input value="yes" id="yes" type="radio" name="radiot" formControlName="radiot"/>
        </label>
        <label for="no" class="customradio"
            ><span>no</span>
            <input value="no" id="no" type="radio" name="radiot" formControlName="radiot" />
        </label>
    </div>
    <div class="form-check__related" *ngIf="radioButtons.radiot.touched">
      DISPLAY ME
    </div>

但是,我无法让它工作并得到

Error: Cannot read property 'radiot' of undefined

最佳答案

直接替换

*ngIf="radioButtons.radiot.touched"

*ngIf="form.controls.radioButtons.touched"

你的问题将会得到解决

https://stackblitz.com/edit/angular-pfctfu

关于javascript - 检查单选按钮是否脏/以 react 形式被触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58819871/

相关文章:

angular - 在使用 ngx-img-cropper 的 Angular 中,如何获取裁剪后的 jpg 或 png 文件,而不是 base64 图像

angular - 如何从 component.ts 文件中的 Angular 2 中的 json 响应获取键/值对

javascript - 尝试在单击按钮时调用 JavaScript 函数,但没有任何反应

javascript - 这个javascript序列到底发生了什么?

javascript - 在不打开的情况下编写 promise 然后调用

javascript - 用 TypeScript 编写的 AngularJS Controller 中的服务注入(inject)问题

javascript - 将插件注册到库

javascript - 在传单 map 控制图层中切换标记的正确方法是什么?

angular - 如何使用 ag-grid 导出到 excel 功能以及 Angular 6 的大数据(50,000 行)(内存不足错误)?

angular - ExpressionChangedAfterItHasBeenCheckedError : Expression has changed after it was checked. 以前的值: 'undefined'