angular - 如果我将 ngIf 与它一起使用,则切换按钮的组属性不起作用
问题描述
当我在切换组中使用 ngIf 时,#group 属性不起作用。
代码:
<mat-button-toggle-group *ngIf="query.noOfQuestions == 05" (change)="toggleChangeQuestion($event)" name="selectQuestions" #group1="matButtonToggleGroup">
<mat-button-toggle *ngIf="test[0].answer==null" class="margin-right" value="0" checked>01</mat-button-toggle>
<mat-button-toggle *ngIf="test[1].answer==null" class="margin-right" value="1">02</mat-button-toggle>
<mat-button-toggle *ngIf="test[2].answer==null" class="margin-right" value="2">03</mat-button-toggle>
<mat-button-toggle *ngIf="test[3].answer==null" class="margin-right" value="3">04</mat-button-toggle>
<mat-button-toggle *ngIf="test[4].answer==null" value="4">05</mat-button-toggle>
</mat-button-toggle-group>
<button (click)="test(group1)">Go</button>
在 TS 文件的测试方法中,我尝试使用 group1.value,它返回错误cannot set property 'value' of undefined。
如果我删除行 * ngIf="query.noOfQuestions == 05" ,代码可以正常工作。
请让我知道是否有任何解决方法?
完整的 HTML 代码:
<div *ngIf="test?.length > 0">
<mat-button-toggle-group *ngIf="query.noOfQuestions == 05" (change)="toggleChangeQuestion($event)" name="selectQuestions" #group1="matButtonToggleGroup">
<mat-button-toggle *ngIf="test[0].answer==null" value="0" checked>01</mat-button-toggle>
<mat-button-toggle *ngIf="test[1].answer==null" value="1">02</mat-button-toggle>
<mat-button-toggle *ngIf="test[2].answer==null" value="2">03</mat-button-toggle>
<mat-button-toggle *ngIf="test[3].answer==null" value="3">04</mat-button-toggle>
<mat-button-toggle *ngIf="test[4].answer==null" value="4">05</mat-button-toggle>
</mat-button-toggle-group>
<mat-button-toggle-group *ngIf="query.noOfQuestions == 10" (change)="toggleChangeQuestion($event)" name="selectQuestions" #group1="matButtonToggleGroup">
<mat-button-toggle *ngIf="test[0].answer==null" value="0" checked>01</mat-button-toggle>
<mat-button-toggle *ngIf="test[1].answer==null" value="1">02</mat-button-toggle>
<mat-button-toggle *ngIf="test[2].answer==null" value="2">03</mat-button-toggle>
<mat-button-toggle *ngIf="test[3].answer==null" value="3">04</mat-button-toggle>
<mat-button-toggle *ngIf="test[4].answer==null" value="4">05</mat-button-toggle>
<mat-button-toggle *ngIf="test[5].answer==null" value="5">06</mat-button-toggle>
<mat-button-toggle *ngIf="test[6].answer==null" value="6">07</mat-button-toggle>
<mat-button-toggle *ngIf="test[7].answer==null" value="7">08</mat-button-toggle>
<mat-button-toggle *ngIf="test[8].answer==null" value="8">09</mat-button-toggle>
<mat-button-toggle *ngIf="test[9].answer==null" value="9">10</mat-button-toggle>
</mat-button-toggle-group>
</div>
<button (click)="test(group1)">Test</button>
解决方案
不是对您的原始问题的答案,而是一个如何实现的示例,*ngFor
这将有助于清理您的 HTML 代码
<mat-button-toggle-group *ngIf="query.noOfQuestions == 10" (change)="toggleChangeQuestion($event)" name="selectQuestions" #group1="matButtonToggleGroup">
<div *ngFor="let item of test; let i = index;">
<mat-button-toggle *ngIf="!item.answer" value="{{i}}">{{i}}</mat-button-toggle>
</div>
</mat-button-toggle-group>
推荐阅读
- javascript - 反应完美的滚动条导致小屏幕尺寸出现问题
- php - laravel 7 sanctum Hash::check 总是返回 false
- python - 在 Python 中使用 Q-learning 和 OpenAI-Gym 时如何从地图中取出对象
- html - 需要 CSS 帮助来停止滚动溢出
- javascript - React Material UI GridList 单元格不能正确换行
- c - 如何使用 clang 格式保持标签缩进?
- delphi - Delphi - 使用 DirectX 从线程捕获网络摄像头快照
- reactjs - React 路由器将我重定向到 Hash Router,而不是返回到基本 url
- security - 加密我的 Web 应用程序中的所有用户数据
- python - 学校作业的问题让 python 写入一个不存在的文件