Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 默认情况下,“角度2检查”单选按钮_Angular - Fatal编程技术网

Angular 默认情况下,“角度2检查”单选按钮

Angular 默认情况下,“角度2检查”单选按钮,angular,Angular,我试图检查默认值为零的单选按钮。我正在尝试使用[checked]=“true”属性。但这是行不通的 <input type="radio" name="unitTrusts-pnl" id="unitTrusts-pnl0" class="with-gap" [value]="0" [(ngModel)]="unitTrustsPnl" [checked]='true'> <input type="radio" name="unitTrusts-pnl" id="unitTr

我试图检查默认值为零的单选按钮。我正在尝试使用
[checked]=“true”
属性。但这是行不通的

<input type="radio" name="unitTrusts-pnl" id="unitTrusts-pnl0" class="with-gap" [value]="0" [(ngModel)]="unitTrustsPnl"  [checked]='true'>
<input type="radio" name="unitTrusts-pnl" id="unitTrusts-pnl0" class="with-gap" [value]="1" [(ngModel)]="unitTrustsPnl">

演示


由于按钮绑定到变量,请尝试设置变量:

unitTrustsPnl = 0;

尝试将此项添加到您已有的[checked]项之外:

[attr.checked]="true"

每个单选按钮都有一个属性calld value,这意味着如果将ngModel的值(unitTrustsPnl)设置为每个单选按钮的值,则将选中该单选按钮

你有

<input type="radio" name="unitTrusts-pnl" id="unitTrusts-pnl0" class="with-gap" [value]="0" [(ngModel)]="unitTrustsPnl">

因此,如果将unitTrustsPnl值设置为0,此收音机将进行检查,依此类推

更新: 您的变量(unitTrustsPnl)应该是数字类型,声明如下

公共单位信托SPNL:编号

其原因是因为您提到[value]=“0”HTML,认为您有一个数字类型变量

执行类似于
[(ngModel)]=“variableWithZeroValue”[选中]=“variableWithZeroValue==0?true:false”
并在类的构造函数中初始化
variableWithZeroValue=0

但是,每当variableWithZeroValue的值等于零时,这将始终选中复选框


希望有帮助。

parent.component.ts

从'@angular/core'导入{Component};
从'@angular/forms'导入{Form,NgForm};
界面性别{
id:编号;
名称:字符串;
标题:字符串;
值:字符串;
}
常量性别列表=[
{id:1,名称:'性别',值:'男性',标题:'男性'},
{id:2,名称:'gender',值:'female',标题:'female'}
];
@组成部分({
选择器:“应用程序开关盒”,
templateUrl:“./parent.component.html”,
样式URL:['./parent.component.css']
})
导出类ParentComponent{
放射科医生:性别[]=性别列表;
构造函数(){}
onSubmit(表格:NgForm):无效{
console.log('onSubmit form',form)
}
}
parent.component.html


提交
无线电组件

从'@angular/core'导入{Componen,Input};
从'@angular/forms'导入{ControlContainer,NgForm};
@组成部分({
选择器:“应用程序收音机”,
templateUrl:'./radio.component.html',
样式URL:['./radio.component.css'],
视图提供程序:[{provide:ControlContainer,useExisting:NgForm}]
})
出口级无线电元件{
@输入()标题:字符串;
@Input()groupName:string;
@Input()值:字符串;
@Input()默认值:字符串;
@Input()是必需的:布尔值;
构造函数(){}
}
radio.component.html


{{title}}

try
[checked]=“'checked'”
@KamilKiełczewski不确定它是有效的HTML,即使是,它也会被ngModel覆盖。只需初始化变量
unitTrustsPnl=0@KamilKiełczewski。感谢您的回复,但它不起作用。在演示url中尝试@KiranDash将其初始化为0(数字,而不是字符串),谢谢您的回答,但不起作用。我在演示url中进行了尝试。谢谢你的回答,但不起作用。我在演示url中尝试了我的错误。它起作用了。它应该是一个数字。我初始化为string.No。它不起作用。请检查此处的演示您的变量应为数字,检查unitTrustsPnl:number=1;我的错。它起作用了。它应该是一个数字。我初始化为字符串。