[英]Angular 2 - ngModel not checking Checkbox
我正在嘗試創建具有性別,男性和其他(caitlyn jenner之類的東西)的性別選擇視圖。
我目前所擁有的是:
在復選框的標簽旁邊放置SVG(復選框將在生產環境中隱藏,現在在plunkr中可見以進行調試)
但是,當我單擊“女性”時,它會將焦點更改為復選框,而不是選中它。
Plunkr: http ://plnkr.co/edit/Z0Dq2sfJaBrE6Mae2Kg1?p = info
碼:
的CSS
svg {
width: 15px; height: 15px;
border: 2px solid #a13b4a;
}
label {
display: inline-block;
padding-left: 1.5em;
position: relative;
cursor: pointer;
color: black;
}
input[type="checkbox"] {
opacity: 0;
width: 15px;
height: 15px;
}
label svg path {
transition: stroke-dashoffset .4s linear;
}
input[type="checkbox"]:checked ~ label svg path {
stroke-dashoffset: 0;
}
input[type="checkbox"]:checked ~ label {
color: #a13b4a;
}
html
<div>
<input type="checkbox" id="male" [(ngModel)]="isMale" name="isMale"/>
<label (click)="select('male')" for="male">Male
<svg (click)="select('male')" viewBox="0 0 60 40" xmlns="http://www.w3.org/2000/svg"><path d="M21,2 C13.4580219,4.16027394
1.62349378,18.3117469 3,19 C9.03653312,22.0182666 25.2482171,10.3758914 30,8 C32.9363621,6.53181896
41.321398,1.67860195 39,4 C36.1186011,6.88139893.11316157,27.1131616 5,29 C10.3223659,34.3223659
30.6434647,19.7426141 35,18 C41.2281047,15.5087581 46.3445303,13.6554697
46,14 C42.8258073,17.1741927 36.9154967,19.650702 33,22 C30.3136243,23.6118254 17,31.162498
17,34 C17,40.4724865 54,12.4064021 54,17
C54,23.7416728 34,27.2286213 34,37"
id="male-path" stroke-width="4" fill="none" stroke="#a14b4a" stroke-dasharray="270" stroke-dashoffset="270"></path></svg>
</label>
</div>
<div>
<input type="checkbox" id="female" [(ngModel)]="isFemale" [checked]="isFemale" name="isFemale"/>
<label for="female" (click)="select('female')">Female
<svg (click)="select('female')" viewBox="0 0 60 40" xmlns="http://www.w3.org/2000/svg"><path d="M21,2 C13.4580219,4.16027394
1.62349378,18.3117469 3,19 C9.03653312,22.0182666 25.2482171,10.3758914 30,8 C32.9363621,6.53181896
41.321398,1.67860195 39,4 C36.1186011,6.88139893.11316157,27.1131616 5,29 C10.3223659,34.3223659
30.6434647,19.7426141 35,18 C41.2281047,15.5087581 46.3445303,13.6554697
46,14 C42.8258073,17.1741927 36.9154967,19.650702 33,22 C30.3136243,23.6118254 17,31.162498
17,34 C17,40.4724865 54,12.4064021 54,17
C54,23.7416728 34,27.2286213 34,37"
id="female-path" stroke-width="4" fill="none" stroke="#a14b4a" stroke-dasharray="270" stroke-dashoffset="270"></path></svg>
</label>
</div>
<div>
<input type="checkbox" id="other" [(ngModel)]="isOther" name="isOther"/>
<label for="other" (click)="select('other')">Other
<svg (click)="select('other')" viewBox="0 0 60 40" xmlns="http://www.w3.org/2000/svg"><path d="M21,2 C13.4580219,4.16027394
1.62349378,18.3117469 3,19 C9.03653312,22.0182666 25.2482171,10.3758914 30,8 C32.9363621,6.53181896
41.321398,1.67860195 39,4 C36.1186011,6.88139893.11316157,27.1131616 5,29 C10.3223659,34.3223659
30.6434647,19.7426141 35,18 C41.2281047,15.5087581 46.3445303,13.6554697
46,14 C42.8258073,17.1741927 36.9154967,19.650702 33,22 C30.3136243,23.6118254 17,31.162498
17,34 C17,40.4724865 54,12.4064021 54,17
C54,23.7416728 34,27.2286213 34,37"
id="other-path" stroke-width="4" fill="none" stroke="#a14b4a" stroke-dasharray="270" stroke-dashoffset="270"></path></svg>
</label>
</div>
TS
public isMale:boolean;
public isFemale:boolean;
public isOther:boolean;
constructor() {
this.isMale = true;
this.isFemale = false;
this.isOther = false;
}
select(gender:string){
console.log("switch");
switch(gender){
case 'male': {
console.log("male");
this.isMale = true;
this.isOther = false;
this.isFemale = false;
break;
}
case 'female' : {console.log("female");
this.isFemale = true;
this.isOther = false;
this.isMale = false;
break;
}
default: {console.log("default");
this.isFemale = false;
this.isMale = false;
this.isOther = true;
}
}
console.log("status: male:"+this.isMale+";;female:"+this.isFemale+";;other:"+this.isOther);
}
您的實現有兩個問題。
[(ngModel)]
,因為您正在從控制器更新值。 只需使用[checked]
指令將狀態反映到復選框即可 (click)
事件為1次) 我用一個可行的例子更新了您的朋克
.html
<input type="radio" name="radioOption" [(ngModel)]="option" id="radio1" value="1"/>
<input type="radio" name="radioOption" [(ngModel)]="option" id="radio2" value="2" />
.ts
export class PaymentMethodComponent implements OnInit {
option: string;
constructor() {
this.option= '1'
}
}
您需要設置變量的值,即option = 1
然后默認情況下將根據提供的值選中您的復選框/單選按鈕。
注意:確保對單選按鈕進行分組,保持名稱與按名稱分組相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.