如何通过changed-eventlistener设置复选框的checked-attribute
How to set a checkbox's checked-attribute via its changed-eventlistener
我在 angular.js 4 中有一个矩阵(带有 material.angular),其中充满了复选框。当用户点击一个复选框时,一个 HTTP 请求被发送到后端服务器。如果后端服务器不可用或服务调用因任何原因出错,我希望复选框 return 恢复到之前的状态 (checked/unchecked)。因此,我实现了以下 change-eventhandler 来进行服务调用并根据服务调用的结果做出反应:
zuordnungChanged(rolle_id: string, recht_id: string) {
let a = this.zuordnungsmatrix["Rolle_" + rolle_id + "_Recht_" + recht_id];
if (a === true ){
this.rechtematrix.rechtEntziehen(rolle_id, recht_id).subscribe(() => {
a = false;
this.zuordnungsmatrix["Rolle_" + rolle_id + "_Recht_" + recht_id] = a;
//Notify user
});
},
() => {
//Notify user
});
} else {
this.rechtematrix.rechtVergeben(rolle_id, recht_id).subscribe(() => {
a = true;
this.zuordnungsmatrix["Rolle_" + rolle_id + "_Recht_" + recht_id] = a;
//Notify User
});
},
() => {
//Notify user
}
);
}
}
zuordnungsmatrix 变量定义和填充如下:
private zuordnungen: Array<any>;
private zuordnungsmatrix = [];
erstelleZuordnungsmatrix(): void {
for (let rolle of this.zuordnungen) {
for ( let recht of rolle.recht_ids) {
this.zuordnungsmatrix["Rolle_" + rolle.rolle_id + "_Recht_" + recht] = true;
}
}
}
我将 zuordnungsmatrix 中的值绑定到复选框的选中属性,如下所示:
<table td-data-table *ngIf="rechteUndRollenVorhanden()">
<tr>
<th td-data-table-column></th>
<th td-data-table-column *ngFor="let rolle of rollen" [id]="'Rolle_' + rolle.rolle_id">{{rolle.beschreibung}}</th>
</tr>
<tr td-data-table-row *ngFor="let recht of rechte" [id]="'Recht_' + recht.recht_id">
<td td-data-table-cell>{{recht.beschreibung}}</td>
<td td-data-table-cell *ngFor="let rolle of rollen">
<md-checkbox [id]="'Rolle_' + rolle.rolle_id + '_Recht_' + recht.recht_id"
[checked]="zuordnungsmatrix['Rolle_' + rolle.rolle_id + '_Recht_' + recht.recht_id] ? true : false"
(change)="zuordnungChanged(rolle.rolle_id, recht.recht_id, $event)">{{zuordnungsmatrix['Rolle_' + rolle.rolle_id + '_Recht_' + recht.recht_id] ? true : false}}
</md-checkbox>
</td>
</tr>
</table>
我知道的问题是我不知道如何让复选框代表 zuordnungsmatrix 的实际状态。变量已更新,视图中的此绑定也有效:
{{zuordnungsmatrix['Rolle_' + rolle.rolle_id + '_Recht_' + recht.recht_id] ? true : false}}
但是我尝试了很多来更新复选框,但是没有用。无论如何,复选框的选中状态的初始设置工作正常。如何在复选框自身的事件侦听器中更新复选框的状态?
解决方法很简单:
出于某种原因 Angular 没有通过单向绑定更新复选框的更改值。您只需显式设置 checked
status 。为此,您可以通过参数 $event
将事件从 HTML 传递到组件。这已经在上面的示例中完成了。
在组件的更改事件处理程序中,您可以读取事件并获取对复选框元素的引用。您所要做的就是设置该元素的 checked 属性 ($event.source.checked
)。
问题中的事件处理程序现在如下所示:
zuordnungChanged(rolle_id: string, recht_id: string, event: any) {
let a = this.zuordnungsmatrix["Rolle_" + rolle_id + "_Recht_" + recht_id];
if (a === true ){
this.rechtematrix.rechtEntziehen(rolle_id, recht_id).subscribe(() => {
//Happy case
a = false;
this.zuordnungsmatrix["Rolle_" + rolle_id + "_Recht_" + recht_id] = a;
//Notify user
});
},
() => {
//Bad case
event.source.checked = this.zuordnungsmatrix['Rolle_' + rolle_id + '_Recht_' + recht_id];
//Notify user
});
} else {
this.rechtematrix.rechtVergeben(rolle_id, recht_id).subscribe(() => {
//Happy case
a = true;
this.zuordnungsmatrix["Rolle_" + rolle_id + "_Recht_" + recht_id] = a;
//Notify User
});
},
() => {
//Bad case
event.source.checked = this.zuordnungsmatrix['Rolle_' + rolle_id + '_Recht_' + recht_id];
//Notify user
}
);
}
}
请注意,属性设置仅在 REST 调用失败时完成。这是因为在快乐的情况下,复选框会通过用户交互自动更新。
我在 angular.js 4 中有一个矩阵(带有 material.angular),其中充满了复选框。当用户点击一个复选框时,一个 HTTP 请求被发送到后端服务器。如果后端服务器不可用或服务调用因任何原因出错,我希望复选框 return 恢复到之前的状态 (checked/unchecked)。因此,我实现了以下 change-eventhandler 来进行服务调用并根据服务调用的结果做出反应:
zuordnungChanged(rolle_id: string, recht_id: string) {
let a = this.zuordnungsmatrix["Rolle_" + rolle_id + "_Recht_" + recht_id];
if (a === true ){
this.rechtematrix.rechtEntziehen(rolle_id, recht_id).subscribe(() => {
a = false;
this.zuordnungsmatrix["Rolle_" + rolle_id + "_Recht_" + recht_id] = a;
//Notify user
});
},
() => {
//Notify user
});
} else {
this.rechtematrix.rechtVergeben(rolle_id, recht_id).subscribe(() => {
a = true;
this.zuordnungsmatrix["Rolle_" + rolle_id + "_Recht_" + recht_id] = a;
//Notify User
});
},
() => {
//Notify user
}
);
}
}
zuordnungsmatrix 变量定义和填充如下:
private zuordnungen: Array<any>;
private zuordnungsmatrix = [];
erstelleZuordnungsmatrix(): void {
for (let rolle of this.zuordnungen) {
for ( let recht of rolle.recht_ids) {
this.zuordnungsmatrix["Rolle_" + rolle.rolle_id + "_Recht_" + recht] = true;
}
}
}
我将 zuordnungsmatrix 中的值绑定到复选框的选中属性,如下所示:
<table td-data-table *ngIf="rechteUndRollenVorhanden()">
<tr>
<th td-data-table-column></th>
<th td-data-table-column *ngFor="let rolle of rollen" [id]="'Rolle_' + rolle.rolle_id">{{rolle.beschreibung}}</th>
</tr>
<tr td-data-table-row *ngFor="let recht of rechte" [id]="'Recht_' + recht.recht_id">
<td td-data-table-cell>{{recht.beschreibung}}</td>
<td td-data-table-cell *ngFor="let rolle of rollen">
<md-checkbox [id]="'Rolle_' + rolle.rolle_id + '_Recht_' + recht.recht_id"
[checked]="zuordnungsmatrix['Rolle_' + rolle.rolle_id + '_Recht_' + recht.recht_id] ? true : false"
(change)="zuordnungChanged(rolle.rolle_id, recht.recht_id, $event)">{{zuordnungsmatrix['Rolle_' + rolle.rolle_id + '_Recht_' + recht.recht_id] ? true : false}}
</md-checkbox>
</td>
</tr>
</table>
我知道的问题是我不知道如何让复选框代表 zuordnungsmatrix 的实际状态。变量已更新,视图中的此绑定也有效:
{{zuordnungsmatrix['Rolle_' + rolle.rolle_id + '_Recht_' + recht.recht_id] ? true : false}}
但是我尝试了很多来更新复选框,但是没有用。无论如何,复选框的选中状态的初始设置工作正常。如何在复选框自身的事件侦听器中更新复选框的状态?
解决方法很简单:
出于某种原因 Angular 没有通过单向绑定更新复选框的更改值。您只需显式设置 checked
status 。为此,您可以通过参数 $event
将事件从 HTML 传递到组件。这已经在上面的示例中完成了。
在组件的更改事件处理程序中,您可以读取事件并获取对复选框元素的引用。您所要做的就是设置该元素的 checked 属性 ($event.source.checked
)。
问题中的事件处理程序现在如下所示:
zuordnungChanged(rolle_id: string, recht_id: string, event: any) {
let a = this.zuordnungsmatrix["Rolle_" + rolle_id + "_Recht_" + recht_id];
if (a === true ){
this.rechtematrix.rechtEntziehen(rolle_id, recht_id).subscribe(() => {
//Happy case
a = false;
this.zuordnungsmatrix["Rolle_" + rolle_id + "_Recht_" + recht_id] = a;
//Notify user
});
},
() => {
//Bad case
event.source.checked = this.zuordnungsmatrix['Rolle_' + rolle_id + '_Recht_' + recht_id];
//Notify user
});
} else {
this.rechtematrix.rechtVergeben(rolle_id, recht_id).subscribe(() => {
//Happy case
a = true;
this.zuordnungsmatrix["Rolle_" + rolle_id + "_Recht_" + recht_id] = a;
//Notify User
});
},
() => {
//Bad case
event.source.checked = this.zuordnungsmatrix['Rolle_' + rolle_id + '_Recht_' + recht_id];
//Notify user
}
);
}
}
请注意,属性设置仅在 REST 调用失败时完成。这是因为在快乐的情况下,复选框会通过用户交互自动更新。