我试图根据Angular 8中的值禁用和启用复选框
这是我的代码
列表数组,例如:这是GetCall。 ktsessions = [{“主持人”:“ bharath”,“ topic”:“ angular”,“状态”:“计划”,“ emailId”:“ [email  email  protected] :“ “,”主题“:“ aps”,“状态”:“计划”,“ emailId”:“ [email  prected] ”}];
<tr *ngFor = "let ktsession of ktsessions >
<td ><input type="checkbox" [disabled]='disabled'></td>
</tr>
TS代码:
getktsession(){
this.service.getKtsession().subscribe(data =>{
console.log('get', data);
this.ktsessions = data;
this.ktsessions.find(user => {
if(user.presenter ==="Kanchan"){
this.disabled = true
} else {
this.disabled = false;
}
});
});
}
Here is my code
I am having List Array like: this is getCall.
ktsessions =
[ {"presenter":"Bharath","topic":"Angular","status":"scheduled","emailId":"[email protected]"}, {"presenter":"Sayyad","topic":"Angular","status":"organized","emailId":"[email protected]"},{"presenter":"Kanchan","topic":"APS","status":"scheduled","emailId":"[email protected]"} ];
<tr *ngFor = "let ktsession of ktsessions >
<td ><input type="checkbox" [disabled]='disabled'></td>
</tr>
TS code:
getktsession(){
this.service.getKtsession().subscribe(data =>{
console.log('get', data);
this.ktsessions = data;
this.ktsessions.find(user => {
if(user.presenter ==="Kanchan"){
this.disabled = true
} else {
this.disabled = false;
}
});
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有几个问题。
&lt; tr *ngfor =“让ktsessions&gt;
的ktsession-在ktsessions
[disabled] =“ ktsession.disabled” 每个复选框实例都需要其自己的&lt; td;禁用] ='disabled'&gt;&lt;/td&gt;
-[disabled] ='disabled'
应该是禁用>禁用
component类实例的属性而不是复选框实例this.disabled = true
和this.disabled = false
- 它们在组件类实例上设置disabled
属性(这是<代码> this 是指),但是您需要做的是在每个禁用
属性上设置属性> user 实例:user.disabled = true 或
user.disabled = false
。因此,您的模板应该看起来像这样:
并且您的
subscribe
应该看起来像这样:这是一个stackblitz 显示它可以与这些更改一起使用。
There are several issues here.
<tr *ngFor = "let ktsession of ktsessions >
- Missing closing quote afterktsessions
<td ><input type="checkbox" [disabled]='disabled'></td>
-[disabled]='disabled'
should be[disabled]="ktsession.disabled"
. Each checkbox instance needs to have its owndisabled
property.[disabled]='disabled'
sets each checkbox'sdisabled
property to thedisabled
property of the component class instance rather than the checkbox instance.this.ktsessions.find(user => {
- Array#find is not an appropriate method to use here, even though it happens to work since you're not returning anything from the callback function and it will therefore iterate the entire array. Array#find is for searching an array for an element that matches the specified criteria and returning that element, not for iterating over an array and setting properties on each element, which is what you're doing here. Array#forEach is the appropriate method for that.this.disabled = true
andthis.disabled = false
- these are setting thedisabled
property on the component class instance (which is whatthis
refers to), but what you need to do is set thedisabled
property on eachuser
instance:user.disabled = true
oruser.disabled = false
.So your template should look something like this:
And your
subscribe
should look something like this:Here's a StackBlitz showing it working with those changes.