Javascript 角度反应形式内的切换按钮
在我的角度反应形式中,我尝试使用三态切换开关,我在链接中分别尝试了三态切换 三态切换堆栈闪电战: 我需要在formarray中的每一行上使用相同的css在被动表单中实现相同的功能 为此,我尝试了相同的html和css,并给出了Javascript 角度反应形式内的切换按钮,javascript,angular,typescript,angular6,angular-reactive-forms,Javascript,Angular,Typescript,Angular6,Angular Reactive Forms,在我的角度反应形式中,我尝试使用三态切换开关,我在链接中分别尝试了三态切换 三态切换堆栈闪电战: 我需要在formarray中的每一行上使用相同的css在被动表单中实现相同的功能 为此,我尝试了相同的html和css,并给出了formcontrolnamelike <div class="switch-toggle switch-3 switch-candy"> <ng-container #buttonIcon *ngFor="let option of opti
formcontrolname
like
<div class="switch-toggle switch-3 switch-candy">
<ng-container #buttonIcon *ngFor="let option of options" >
<input type="radio" formControlName="state" [checked]="value === option"
[value]="option" />
<label (click)="onSelectionChange(option)" for="{{option}}">{{option.id}}
</label></ng-container> <a></a>
</div>
{{option.id}
请点击第二次stackblitz中的add按钮查看结果。。在app.component.css
中添加了css
请帮助我将第一个stackblitz中的三态切换开关实现为第二个stackblitz中的被动形式,并需要获取所选切换的值。被动组件中的选项数组不同于您实现的三向开关。真实组件中的一个没有id
[
"on",
"na",
"off"
]
您仍然可以这样使用它:
<ng-container #buttonIcon *ngFor="let option of options" >
<input type="radio" formControlName="state" [checked]="value === option"
[value]="option" />
<label (click)="onSelectionChange(option)" for="{{option}}">
{{option}} //**refer the object as the id filed is missing**
</label>
<div
class="switch-toggle switch-3 switch-candy">
<ng-container
#buttonIcon
*ngFor="let option of options">
<input
type="radio"
formControlName="state"
[value]="option"
[id]="i+option" />
<label
[attr.for]="i+option">
{{option}}
</label>
</ng-container>
<a></a>
</div>
{{option}}/**引用对象,因为缺少id文件**
第二个区别是,您缺少无功组件中的onSelectionChange
功能无功组件中的选项数组与您实现的三向开关不同。真实组件中的一个没有id
[
"on",
"na",
"off"
]
您仍然可以这样使用它:
<ng-container #buttonIcon *ngFor="let option of options" >
<input type="radio" formControlName="state" [checked]="value === option"
[value]="option" />
<label (click)="onSelectionChange(option)" for="{{option}}">
{{option}} //**refer the object as the id filed is missing**
</label>
<div
class="switch-toggle switch-3 switch-candy">
<ng-container
#buttonIcon
*ngFor="let option of options">
<input
type="radio"
formControlName="state"
[value]="option"
[id]="i+option" />
<label
[attr.for]="i+option">
{{option}}
</label>
</ng-container>
<a></a>
</div>
{{option}}/**引用对象,因为缺少id文件**
第二个区别是,您缺少反应组件中的onSelectionChange
功能,只需向输入标记添加[id]
,向标签标记添加[attr.for]
。大概是这样的:
<ng-container #buttonIcon *ngFor="let option of options" >
<input type="radio" formControlName="state" [checked]="value === option"
[value]="option" />
<label (click)="onSelectionChange(option)" for="{{option}}">
{{option}} //**refer the object as the id filed is missing**
</label>
<div
class="switch-toggle switch-3 switch-candy">
<ng-container
#buttonIcon
*ngFor="let option of options">
<input
type="radio"
formControlName="state"
[value]="option"
[id]="i+option" />
<label
[attr.for]="i+option">
{{option}}
</label>
</ng-container>
<a></a>
</div>
{{option}}
给你的裁判
感谢A.Winnen对使用上述方法的性能影响的评论。只需在输入标记中添加一个[id]
,在标签标记中添加一个[attr.for]
。大概是这样的:
<ng-container #buttonIcon *ngFor="let option of options" >
<input type="radio" formControlName="state" [checked]="value === option"
[value]="option" />
<label (click)="onSelectionChange(option)" for="{{option}}">
{{option}} //**refer the object as the id filed is missing**
</label>
<div
class="switch-toggle switch-3 switch-candy">
<ng-container
#buttonIcon
*ngFor="let option of options">
<input
type="radio"
formControlName="state"
[value]="option"
[id]="i+option" />
<label
[attr.for]="i+option">
{{option}}
</label>
</ng-container>
<a></a>
</div>
{{option}}
给你的裁判
感谢A.Winnen对使用前一种方法的性能影响的评论。这里有一个stackblitz演示供您参考。
这里有一个stackblitz演示供您参考。
我已经在调查了。你的三向开关在反应形式下似乎不起作用。是的,你是对的,它在反应形式下不起作用。。请帮我把三态开关包括在无功表中,我已经在研究了。你的三向开关在反应形式下似乎不起作用。是的,你是对的,它在反应形式下不起作用。。请帮我把三态开关包括在无功表中。对不起,遗漏了一件事。。默认情况下,所有行都需要选择na。在推送新的FormGroup
时,只需在您的addCreds
方法中添加状态:“na”
,我现在只能在手机中签入,以便我看到我需要的东西没有问题。。如果有任何疑问能让您知道,我将接受您的回答。@undefined,no problem。很高兴我能帮忙。:)@SiddAjmera,每次我在反应形式中看到一个(ngModelChange),我都会发疯:)对不起,遗漏了一件事。。默认情况下,所有行都需要选择na。在推送新的FormGroup
时,只需在您的addCreds
方法中添加状态:“na”
,我现在只能在手机中签入,以便我看到我需要的东西没有问题。。如果有任何疑问能让您知道,我将接受您的回答。@undefined,no problem。很高兴我能帮忙。:)@SiddAjmera,每次我在反应形式中看到(ngModelChange),我都会发疯:)