当用户勾选复选框时,是否有快速解决方案可以移除true/false
框中的input
值,以便显示默认placeholder
而不是值true/false
希望可以使用inline
字段使用input
代码来完成,我们的想法就是隐藏/显示文字,但当checkbox
为true
时它会放置输入框中的值
答案 0 :(得分:2)
试试此代码
Html
<label>Name:</label>
<!-- data-bind to the input element; store value in yourName -->
<input type="text" [(ngModel)]="yourName" placeholder="Enter a name here">
<input type="checkbox" [(ngModel)]="reset" placeholder="Enter a name here" (click)="removeModelValue()">
<hr>
<!-- conditionally display `yourName` -->
<h1 [hidden]="!reset">Hello {{yourName}}!</h1>
ts
export class HelloWorld {
// Declaring the variable for binding with initial value
yourName: string = '';
reset: boolean = true;
removeModelValue() {
this.yourName = '';
}
}
答案 1 :(得分:2)
哟可以尝试更改复选框的模型,如“&#34;已检查&#34;
”<label>Name:</label>
<!-- data-bind to the input element; store value in yourName -->
<input type="text" [(ngModel)]="yourName" placeholder="Enter a name here">
<input type="checkbox" [(ngModel)]="checked" placeholder="Enter a name here">
<hr>
<!-- conditionally display `yourName` -->
<h1 [hidden]="!checked || !yourName|| checked==null ">Hello {{yourName}}!</h1>
您可以查看此Plank
答案 2 :(得分:1)
更改您的代码如下:
<强> HTML:强>
<label>Name:</label>
<!-- data-bind to the input element; store value in yourName -->
<input type="text" [(ngModel)]="yourName" (ngModelChange)="valuechange($event)" placeholder="Enter a name here">
<input type="checkbox" [(ngModel)]="isChecked" placeholder="Enter a name here">
<hr>
<!-- conditionally display `yourName` -->
<h1 [hidden]="isChecked==false || isChecked==null">Hello {{yourName}}!</h1>
<强>组件(TS)强>
export class HelloWorld {
isChecked:boolean=false;
// Declaring the variable for binding with initial value
yourName: string = '';
valuechange(newValue) {
mymodel = newValue;
if(mymodel!='')
{
this.isChecked=true;
}
else
{
this.isChecked=false;
}
}
}